<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="玖忆;文鹤;博客">
    
    <meta name="author" content="meteor">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://wait-you.github.io/2023/06/06/spring-cloud-技术栈/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="# Spring Cloud 技术栈 # 版本选择 # SpringBoot 版本选择  git 源码地址  https:&#x2F;&#x2F;github.com&#x2F;spring-projects&#x2F;spring-boot&#x2F;releases&#x2F;    官网看 Boot 版本  # SpringCloud 版本选择  git 源码地址  https:&#x2F;&#x2F;github.com&#x2F;spring-projects&#x2F;spring-">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Cloud 技术栈">
<meta property="og:url" content="https://wait-you.github.io/2023/06/06/Spring-Cloud-%E6%8A%80%E6%9C%AF%E6%A0%88/index.html">
<meta property="og:site_name" content="玖忆">
<meta property="og:description" content="# Spring Cloud 技术栈 # 版本选择 # SpringBoot 版本选择  git 源码地址  https:&#x2F;&#x2F;github.com&#x2F;spring-projects&#x2F;spring-boot&#x2F;releases&#x2F;    官网看 Boot 版本  # SpringCloud 版本选择  git 源码地址  https:&#x2F;&#x2F;github.com&#x2F;spring-projects&#x2F;spring-">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220114211406758.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220115190200323.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220115190222256.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220115190317819.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220115210250803.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220115212752880.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220117151551910.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220117164451853.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220119230024082.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220119230112586.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220121173321528.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220121190645557.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220121191209732.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220127114926407.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220127115026032.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220128170048526.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220130111911145.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220130114025348.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220205170840600.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220205171351345.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220205215859164.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220206155229696.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220206173145685.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220208152334509.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220208152556235.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220208152953670.png">
<meta property="og:image" content="http://tuchuang.wenhe9.cn/image-20220208153436295.png">
<meta property="article:published_time" content="2023-06-06T01:05:40.000Z">
<meta property="article:modified_time" content="2023-06-05T09:06:07.832Z">
<meta property="article:author" content="meteor">
<meta property="article:tag" content="Spring Cloud">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://tuchuang.wenhe9.cn/image-20220114211406758.png">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/images/wenhe.png" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/images/wenhe.png">
    <meta name="theme-color" content="#f1404b">
    <link rel="shortcut icon" href="/images/wenhe.png">
    <!--- Page Info-->
    
    <title>
        
            Spring Cloud 技术栈 -
        
        玖忆
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    
<link rel="stylesheet" href="/assets/fonts.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"wait-you.github.io","root":"/","language":"zh-CN"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"placeholder":"http://tuchuang.wenhe9.cn/default-bg.jpg","skip_dirs":[]}},"colors":{"primary":"#f1404b","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":true,"percentage":true},"busuanzi_counter":{"enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"pjax":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"static","image":{"light":"http://tuchuang.wenhe9.cn/default-bg.jpg","dark":"http://tuchuang.wenhe9.cn/default-bg.jpg"},"title":"玖忆","subtitle":{"text":["我本微末凡尘、可也心向天空"],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"links":{"github":"https://gitee.com/du-jinliang","instagram":null,"zhihu":null,"twitter":null,"email":"dujinliang9@163.com"}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":false,"type":"fixed","audios":[{"name":null,"artist":null,"url":null,"cover":null}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.1.4","navbar":{"auto_hide":true,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":null},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}}};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="main-content-container">

        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                
                玖忆
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer">
        <ul class="drawer-navbar-list">
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            
            
                <div class="article-title">
                    <h1 class="article-title-regular">Spring Cloud 技术栈</h1>
                </div>
            
                
            

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/wenhe.png">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">meteor</span>
                            
                                <span class="author-label">Lv3</span>
                            
                        </div>
                        <div class="meta-info">
                            <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2023-06-05 17:05:40</span>
        <span class="mobile">2023-06-05 17:05</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-06-05 01:06:07</span>
            <span class="mobile">2023-06-05 01:06</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/%E5%BE%AE%E6%9C%8D%E5%8A%A1/">微服务</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fa-regular fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/Spring-Cloud/">Spring Cloud</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content markdown-body">
                <h1 id="spring-cloud-技术栈"><a class="markdownIt-Anchor" href="#spring-cloud-技术栈">#</a> Spring Cloud 技术栈</h1>
<h2 id="版本选择"><a class="markdownIt-Anchor" href="#版本选择">#</a> 版本选择</h2>
<h3 id="springboot版本选择"><a class="markdownIt-Anchor" href="#springboot版本选择">#</a> SpringBoot 版本选择</h3>
<ul>
<li>git 源码地址
<ul>
<li><a class="link"   target="_blank" rel="noopener" href="https://github.com/spring-projects/spring-boot/releases/" >https://github.com/spring-projects/spring-boot/releases/ <i class="fa-regular fa-arrow-up-right-from-square fa-sm"></i></a></li>
</ul>
</li>
<li>官网看 Boot 版本</li>
</ul>
<h3 id="springcloud版本选择"><a class="markdownIt-Anchor" href="#springcloud版本选择">#</a> SpringCloud 版本选择</h3>
<ul>
<li>git 源码地址
<ul>
<li><a class="link"   target="_blank" rel="noopener" href="https://github.com/spring-projects/spring-cloud" >https://github.com/spring-projects/spring-cloud <i class="fa-regular fa-arrow-up-right-from-square fa-sm"></i></a></li>
</ul>
</li>
<li>官网看 cloud 版本</li>
</ul>
<h3 id="springboot-和-springcloud版本对应查看"><a class="markdownIt-Anchor" href="#springboot-和-springcloud版本对应查看">#</a> SpringBoot 和 SpringCloud 版本对应查看</h3>
<ul>
<li>查看 json 返回的结果
<ul>
<li><a class="link"   target="_blank" rel="noopener" href="https://start.spring.io/actuator/info" >https://start.spring.io/actuator/info <i class="fa-regular fa-arrow-up-right-from-square fa-sm"></i></a></li>
</ul>
</li>
</ul>
<h2 id="关于cloud各种组件的停更升级替换"><a class="markdownIt-Anchor" href="#关于cloud各种组件的停更升级替换">#</a> 关于 Cloud 各种组件的停更 / 升级 / 替换</h2>
<blockquote>
<p>Cloud 升级</p>
</blockquote>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220114211406758.png"
                      alt="image-20220114211406758"
                ></p>
<ul>
<li>
<p><strong>服务注册中心</strong></p>
<ul>
<li>Eureka
<ul>
<li>停更</li>
</ul>
</li>
<li>Zookeeper
<ul>
<li>做最少的切换，技术是最熟悉的</li>
<li>不想用新技术，只想用老技术的选择</li>
</ul>
</li>
<li>Consul
<ul>
<li>不推荐</li>
</ul>
</li>
<li>Nacos
<ul>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>服务调用</strong></p>
<ul>
<li>Ribbon
<ul>
<li>维护状态</li>
<li>等待被替换</li>
</ul>
</li>
<li>LoadBalancer
<ul>
<li>发芽阶段</li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong>服务调用</strong></p>
<ul>
<li>Fegin
<ul>
<li>挂了</li>
</ul>
</li>
<li>OpenFeign
<ul>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
<li>
<p>服务降级</p>
<ul>
<li>
<p>Hystrix</p>
<ul>
<li>不推荐</li>
</ul>
</li>
<li>
<p>resilience4j</p>
</li>
<li>
<p>sentienl</p>
<ul>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
<li>
<p>服务网关</p>
<ul>
<li>Zuul
<ul>
<li>挂了</li>
</ul>
</li>
<li>Zuul2
<ul>
<li>胎死腹中</li>
</ul>
</li>
<li>gateway
<ul>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
<li>
<p>服务配置</p>
<ul>
<li>config
<ul>
<li>不推荐</li>
</ul>
</li>
<li>apolo</li>
<li>Nacos
<ul>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
<li>
<p>服务总线</p>
<ul>
<li>Bus
<ul>
<li>不推荐</li>
</ul>
</li>
<li>Nacos
<ul>
<li>推荐</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="微服务架构编码构建"><a class="markdownIt-Anchor" href="#微服务架构编码构建">#</a> 微服务架构编码构建</h2>
<ul>
<li><span style = 'background-color : #f00'>约定 &gt; 配置 &gt; 编码</span></li>
</ul>
<h3 id="idea新建project工作空间"><a class="markdownIt-Anchor" href="#idea新建project工作空间">#</a> IDEA 新建 Project 工作空间</h3>
<ul>
<li>
<p><code>pom.xml</code></p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>cn.wenhe9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>cn.wenhe9.springcloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>packaging</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>packaging</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre>  <span class="token comment">&lt;!-- 统一管理 jar 包版本 --></span></pre></td></tr><tr><td data-num="13"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project.build.sourceEncoding</span><span class="token punctuation">></span></span>utf-8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project.build.sourceEncoding</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.source</span><span class="token punctuation">></span></span>1.8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.source</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.target</span><span class="token punctuation">></span></span>1.8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.target</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>junit.version</span><span class="token punctuation">></span></span>4.12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>junit.version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>log4j.version</span><span class="token punctuation">></span></span>1.2.17<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>log4j.version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>lombok.version</span><span class="token punctuation">></span></span>1.16.18<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>lombok.version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mysql.version</span><span class="token punctuation">></span></span>8.0.24<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mysql.version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="21"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>druid.version</span><span class="token punctuation">></span></span>1.1.16<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>druid.version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mybatis.spring.boot.version</span><span class="token punctuation">></span></span>1.3.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>mybatis.spring.boot.version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="23"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="24"></td><td><pre></pre></td></tr><tr><td data-num="25"></td><td><pre>  <span class="token comment">&lt;!-- 子模块继承之后，提供作用：锁定版本 + 子 module 不用写 groupId 和 version--></span></pre></td></tr><tr><td data-num="26"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencyManagement</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="28"></td><td><pre>      <span class="token comment">&lt;!--spring boot 2.2.2--></span></pre></td></tr><tr><td data-num="29"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="30"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="31"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="32"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.3.12.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="33"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="34"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="35"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="36"></td><td><pre></pre></td></tr><tr><td data-num="37"></td><td><pre>      <span class="token comment">&lt;!--spring cloud Hoxton.SR12--></span></pre></td></tr><tr><td data-num="38"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="39"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="40"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="41"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>Hoxton.SR12<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="42"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="43"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="44"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="45"></td><td><pre></pre></td></tr><tr><td data-num="46"></td><td><pre>      <span class="token comment">&lt;!--spring cloud alibaba 2.1.0.RELEASE--></span></pre></td></tr><tr><td data-num="47"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="48"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="49"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-alibaba-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="50"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>2.1.0.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="51"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">></span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="52"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">></span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="53"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="54"></td><td><pre></pre></td></tr><tr><td data-num="55"></td><td><pre>      <span class="token comment">&lt;!--mysql--></span></pre></td></tr><tr><td data-num="56"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="57"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="58"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="59"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>$&#123;mysql.version&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="60"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="61"></td><td><pre></pre></td></tr><tr><td data-num="62"></td><td><pre>      <span class="token comment">&lt;!--druid--></span></pre></td></tr><tr><td data-num="63"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="64"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="65"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>druid<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="66"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>$&#123;druid.version&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="67"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="68"></td><td><pre></pre></td></tr><tr><td data-num="69"></td><td><pre>      <span class="token comment">&lt;!--mybatis-spring-boot--></span></pre></td></tr><tr><td data-num="70"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="71"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.mybatis.spring.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="72"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>mybatis-spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="73"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>$&#123;mybatis.spring.boot.version&#125;<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="74"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="75"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="76"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencyManagement</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="77"></td><td><pre></pre></td></tr><tr><td data-num="78"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="79"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="80"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="81"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="82"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="83"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>configuration</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="84"></td><td><pre>          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>fork</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>fork</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="85"></td><td><pre>          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>addResources</span><span class="token punctuation">></span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>addResources</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="86"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>configuration</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="87"></td><td><pre>      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="88"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="89"></td><td><pre>  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="90"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="91"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">- 项目功能</span><br><span class="line">  - ![微信截图_20220114220759](http://tuchuang.wenhe9.cn/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20220114220759.png)</span><br><span class="line">- 采用模块化构建</span><br><span class="line">- 采用``restful``API风格</span><br><span class="line">- 将公共类做抽取</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">## Eureka服务注册与发现</span><br><span class="line"></span><br><span class="line">### Eureka基础知识</span><br><span class="line"></span><br><span class="line">&gt; 什么是服务治理</span><br><span class="line"></span><br><span class="line">- Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块实现服务治理</span><br><span class="line">- 在传统的rpc远程调用框架中，管理每个服务与服务之间依赖关系比较复杂，管理比较复杂，所以需要使用服务治理，管理服务与服务之间的依赖关系，可以实现服务调用、负载均衡、容错等，实现服务发现与注册</span><br><span class="line">  -  N多个服务消费者，N多个服务提供者，你调我我调你，需要管理机制，来更好的提供实现 </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&gt; 什么是服务注册与发现</span><br><span class="line"></span><br><span class="line">- Eureka采用了CS的设计架构，Eureka Server作为服务注册功能的服务器，他是服务注册中心，而系统中的其他微服务，使用Eureka的客户端连接到Eureka Server并维持心跳。这样系统的维护人员就可以通过Eureka Server 来监控系统中各个微服务是否正常运行。</span><br><span class="line">- 在服务注册与发现中，有一个注册中心。当服务器启动的时候，会把当前自己服务器的信息，比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者)，以该别名的方式去注册中心上获取到实际的服务通讯地址，然后再实现本地RPC调用RPC远程调用框架核心设计思想：在于注册中心，因为使用注册中心管理每个服务与服务之间的一个依赖关系（服务治理概念）。在任何rpc远程框架中，都会有一个注册中心（存放服务地址相关的信息（接口地址））</span><br><span class="line">- ![image-20220115144812970](http://tuchuang.wenhe9.cn/image-20220115144812970.png)</span><br><span class="line"></span><br><span class="line">&gt; Eureka两组件</span><br><span class="line"></span><br><span class="line">- Eureka包含两个组件：Eureka Server 和 Eureka Client</span><br><span class="line">- Eureka Server提供服务注册服务</span><br><span class="line">  - 各个微服务节点通过配置启动后，会在EurekaServer中进行注册，这样的EurekaServer中的服务注册表中将会存储所有可用服务节点的信息，服务节点的信息可以在界面中直观的看到</span><br><span class="line">- EurekaClient通过注册中心进行访问</span><br><span class="line">  - 是一个Java客户端，用于简化Eureka Server的交互，客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后，将会向Eureka Server发送心跳（默认周期为30秒）。如果EurekaServer在多个心跳周期内没有接收到某个节点的心跳，EurekaServer将会从服务注册表中把这个服务节点移除（90秒）</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">### 单机Eureka构建步骤</span><br><span class="line"></span><br><span class="line">- 导入依赖</span><br><span class="line"></span><br><span class="line">  - 服务端</span><br><span class="line"></span><br><span class="line">    - ```xml</span><br><span class="line">      &lt;dependency&gt;</span><br><span class="line">          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;</span><br><span class="line">          &lt;artifactId&gt;spring-cloud-starter-netflix-eureka-server&lt;/artifactId&gt;</span><br><span class="line">      &lt;/dependency&gt;</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
<li>
<p>客户端</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-eureka-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- yml进行配置</span><br><span class="line"></span><br><span class="line">  - 服务端</span><br><span class="line"></span><br><span class="line">    - ```yaml</span><br><span class="line">      eureka:</span><br><span class="line">        instance:</span><br><span class="line">          hostname: localhost</span><br><span class="line">        client:</span><br><span class="line">          # false 表示不向注册中心注册自己</span><br><span class="line">          register-with-eureka: false</span><br><span class="line">          # false表示自己端就是注册中心，我的职责就是维护服务实例，并不需要去检索服务</span><br><span class="line">          fetch-registry: false</span><br><span class="line">          service-url:</span><br><span class="line">            defalutZone: http://$&#123;eureka.instance.hostname&#125;:$&#123;server.port&#125;/eureka/</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>客户端</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token comment"># 表示是否将自己注册到 EurekaServer 默认为 true</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token comment"># 是否从 EurekaServer 抓取自己的注册信息，默认为 true，单节点无所谓，集群必须配置为 true 才能配合 ribbon 使用负载均衡</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span>7001/eureka/</pre></td></tr><tr><td data-num="9"></td><td><pre>&lt;<span class="token tag">!--code</span>￼2<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
<li>
<p>注册中心二</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">7002</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>eureka<span class="token punctuation">-</span>server<span class="token punctuation">-</span><span class="token number">7002</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token key atrule">hostname</span><span class="token punctuation">:</span> eureka7002.com</pre></td></tr><tr><td data-num="9"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka</pre></td></tr><tr><td data-num="14"></td><td><pre>&lt;<span class="token tag">!--code</span>￼3<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
<li>
<p>服务提供者集群只需要更改启动的端口号即可，应用名称就是集群的名称</p>
</li>
</ul>
</li>
</ul>
<h3 id="actuator微服务信息完整"><a class="markdownIt-Anchor" href="#actuator微服务信息完整">#</a> actuator 微服务信息完整</h3>
<ul>
<li>
<p>主机名称：服务名称修改</p>
<ul>
<li>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220115190200323.png"
                      alt="image-20220115190200323"
                ></p>
</li>
<li>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220115190222256.png"
                      alt="image-20220115190222256"
                ></p>
</li>
</ul>
</li>
<li>
<p>访问信息有 IP 信息提示</p>
<ul>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220115190317819.png"
                      alt="image-20220115190317819"
                ></li>
<li>这样右下角有 ip 提示</li>
</ul>
</li>
</ul>
<h3 id="服务发现discovery"><a class="markdownIt-Anchor" href="#服务发现discovery">#</a> 服务发现 Discovery</h3>
<ul>
<li>
<p>对于注册进 eureka 里面的微服务，可以通过服务发现来获得该服务的信息</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/payment/discovery"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">getServices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> services <span class="token operator">=</span> discoveryClient<span class="token punctuation">.</span><span class="token function">getServices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> service <span class="token operator">:</span> services<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"service : &#123;&#125;"</span><span class="token punctuation">,</span> service<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ServiceInstance</span><span class="token punctuation">></span></span> instanceList <span class="token operator">=</span> discoveryClient<span class="token punctuation">.</span><span class="token function">getInstances</span><span class="token punctuation">(</span><span class="token string">"CLOUD-PAYMENT=SERVICE"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">ServiceInstance</span> serviceInstance <span class="token operator">:</span> instanceList<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span>serviceInstance<span class="token punctuation">.</span><span class="token function">getServiceId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> serviceInstance<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> serviceInstance<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\t"</span> <span class="token operator">+</span> serviceInstance<span class="token punctuation">.</span><span class="token function">getUri</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>discoveryClient<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">4</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="eureka-自我保护"><a class="markdownIt-Anchor" href="#eureka-自我保护">#</a> Eureka 自我保护</h3>
<ul>
<li>
<p>故障现象</p>
<ul>
<li>概述
<ul>
<li>保护模式主要用于一组客户端和 EurekaServer 之间存在网络分区场景下的保护</li>
<li>一旦进入保护模式，<span style="background-color : #f00">EurekaServer 将会尝试保护其他服务注册表中的信息，不再删除服务注册表中的数据，也就是不会注销任何微服务</span></li>
<li>如果在 EurekaServer 的首页看到以下这段信息，则说明 Eureka 进入了保护模式</li>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220115210250803.png"
                      alt="image-20220115210250803"
                ></li>
</ul>
</li>
</ul>
</li>
<li>
<p>导致原因</p>
<ul>
<li>某时刻某一个微服务不可用了，Eureka 不会立刻清理依旧会对微服务的信息进行保存</li>
<li>属于 CAP 里面的 AP 分支</li>
</ul>
</li>
<li>
<p>为什么会产生 Eureka 自我保护机制？</p>
<ul>
<li>为了防止 EurekaClient 可以正常运行，但是与 EurekaServer 网络不通情况下，EurekaServer 不会立刻将 EurekaClient 服务剔除</li>
</ul>
</li>
<li>
<p>什么是自我保护模式？</p>
<ul>
<li>默认情况下，如果 EurekaServer 在一定时间内没有接收到某个微服务实例的心跳，EurekaServer 将会注销实例（默认 90 秒）。但是当网络分区故障发生时（延时、卡顿、拥挤）时，微服务与 EurekaServer 之间无法正常通信，以上行为可能变得非常危险了 —— 因为微服务本事其实是健康的，此时不应该注销这个服务，Eureka 通过 “自我保护模式” 来解决这个问题 —— 当 EurekaServer 节点在短时间内丢失过多客户端时（可能发生了网络分区故障），那么这个节点就会进入自我保护模式</li>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220115212752880.png"
                      alt="image-20220115212752880"
                ></li>
</ul>
</li>
<li>
<p><span style="background-color : #f00">在自我保护模式中，EurekaServer 会保护服务注册表中的信息，不再注销任何服务实例</span></p>
</li>
<li>
<p>他的涉及哲学就是宁可保留错误的服务注册信息，也不盲目的注销任何可能健康的服务实例</p>
</li>
<li>
<p>综上，自我保护模式是一种应对网络异常的安全保护措施，他的架构哲学是宁可同时保留所有微服务（健康的微服务和不健康的微服务都会保留）也不盲目的注销任何健康的微服务。使用自我保护模式，可以让 Eureka 集群更加的健壮和稳定.</p>
</li>
<li>
<p>关闭自我保护</p>
<ul>
<li>
<p>注册中心</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token comment"># 关闭自我保护机制，保证不可用服务被及时删除</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">enable-self-preservation</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token key atrule">eviction-interval-timer-in-ms</span><span class="token punctuation">:</span> <span class="token number">2000</span></pre></td></tr><tr><td data-num="5"></td><td><pre>&lt;<span class="token tag">!--code</span>￼5<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="zookeeper服务注册与发现"><a class="markdownIt-Anchor" href="#zookeeper服务注册与发现">#</a> Zookeeper 服务注册与发现</h2>
<ul>
<li>
<p>启动 Zookeeper 服务，自己搭建或者使用 docker 均可</p>
</li>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-zookeeper-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 配置（服务提供者和消费者一样）</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    server:</span><br><span class="line">      port: 8004</span><br><span class="line">    spring:</span><br><span class="line">      application:</span><br><span class="line">        name: cloud-provider-payment</span><br><span class="line">      cloud:</span><br><span class="line">        zookeeper:</span><br><span class="line">          connect-string: 39.106.88.176:2181</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
<li>
<p>同样是根据 <code>spring.application.name</code>  来区分的</p>
</li>
</ul>
</li>
<li>
<p>主类需要 <code>@EnableDiscoveryClient</code>  注解</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>client<span class="token punctuation">.</span>discovery<span class="token punctuation">.</span></span><span class="token class-name">EnableDiscoveryClient</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="8"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="9"></td><td><pre> * 2022/1/16</pre></td></tr><tr><td data-num="10"></td><td><pre> * @EnableDiscoveryClient 该注解用于向使用 consul 或者 zookeeper 作为注册中心时注册服务</pre></td></tr><tr><td data-num="11"></td><td><pre> */</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token annotation punctuation">@EnableDiscoveryClient</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PaymentMain8004</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">PaymentMain8004</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">7</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>调用</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span>extern<span class="token punctuation">.</span>slf4j<span class="token punctuation">.</span></span><span class="token class-name">Slf4j</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">GetMapping</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestTemplate</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Resource</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="11"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="12"></td><td><pre> * 2022/1/16</pre></td></tr><tr><td data-num="13"></td><td><pre> */</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token annotation punctuation">@Slf4j</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderZKController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> <span class="token constant">INVOKE_URL</span> <span class="token operator">=</span> <span class="token string">"http://cloud-provider-payment"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token annotation punctuation">@Resource</span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="21"></td><td><pre></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/consumer/payment/zk"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="23"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="24"></td><td><pre>        <span class="token class-name">String</span> result <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token constant">INVOKE_URL</span> <span class="token operator">+</span> <span class="token string">"/payment/zk"</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="25"></td><td><pre></pre></td></tr><tr><td data-num="26"></td><td><pre>        <span class="token keyword">return</span> result<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="28"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="29"></td><td><pre></pre></td></tr><tr><td data-num="30"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">8</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8006</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> consul<span class="token punctuation">-</span>provider<span class="token punctuation">-</span>payment</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">consul</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">host</span><span class="token punctuation">:</span> 39.106.88.176</pre></td></tr><tr><td data-num="9"></td><td><pre>      <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8500</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token key atrule">service-name</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>spring.application.name<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token key atrule">heartbeat</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>          <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token key atrule">prefer-ip-address</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="15"></td><td><pre>&lt;<span class="token tag">!--code</span>￼9<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h2 id="openfeign服务调用"><a class="markdownIt-Anchor" href="#openfeign服务调用">#</a> OpenFeign 服务调用</h2>
<h3 id="概述"><a class="markdownIt-Anchor" href="#概述">#</a> 概述</h3>
<blockquote>
<p>OpenFeign 是什么</p>
</blockquote>
<ul>
<li>Feign 是一个声明式的 Web 服务客户端，让编写 Web 服务客户端变得非常容易，只需要创建一个接口并在接口上添加注解即可</li>
</ul>
<blockquote>
<p>Feign 能干什么</p>
</blockquote>
<ul>
<li>Feign 旨在使编写 Java Http 客户端变得容易</li>
<li>前面在使用 Ribbon+RestTemplate 时，利用 RestTemplate 对 http 请求的封装处理，形成了一套模板化的调用方法。但是实际开发中，由于对服务依赖的调用可能不止一处，往往一个接口会被多处调用，所以通常都会针对每隔微服务自行封装一些客户端类来包装这些服务的调用，所以，Feign 在此基础上做了进一步封装由他来帮助我们定义和实现依赖服务接口的定义，在 Feign 的实现下，我们只需要创建一个接口并使用注解的方式来配置它（以前是 Dao 接口上面标注 Mapper 注解，现在是一个微服务接口上面标注一个 Feign 注解即可），即可完成对服务提供方的接口绑定，简化了使用 Spring Cloud Ribbon 时，自动封装服务调用客户端的开发量</li>
<li>Feign 集成了 Ribbon
<ul>
<li>利用 Ribbon 维护了 Payment 的服务列表信息，并且通过轮询实现了客户端的负载均衡，而与 Ribbon 不同的是，通过 feign 只需要定义服务绑定接口且以声明式的方法，优雅简介的实现了服务调用</li>
</ul>
</li>
</ul>
<blockquote>
<p>Feign 和 OpenFeign 的区别</p>
</blockquote>
<ul>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220117151551910.png"
                      alt="image-20220117151551910"
                ></li>
</ul>
<h3 id="openfeign使用步骤"><a class="markdownIt-Anchor" href="#openfeign使用步骤">#</a> OpenFeign 使用步骤</h3>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-openfeign<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    </span><br><span class="line"></span><br><span class="line">- 主启动</span><br><span class="line"></span><br><span class="line">- ```java</span><br><span class="line">  package cn.wenhe9.springcloud;</span><br><span class="line">  </span><br><span class="line">  import org.springframework.boot.SpringApplication;</span><br><span class="line">  import org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line">  import org.springframework.cloud.client.discovery.EnableDiscoveryClient;</span><br><span class="line">  import org.springframework.cloud.netflix.eureka.EnableEurekaClient;</span><br><span class="line">  import org.springframework.cloud.openfeign.EnableFeignClients;</span><br><span class="line">  </span><br><span class="line">  /**</span><br><span class="line">   * @author DuJinliang</span><br><span class="line">   * 2022/1/17</span><br><span class="line">   */</span><br><span class="line">  @SpringBootApplication</span><br><span class="line">  @EnableFeignClients</span><br><span class="line">  public class OpenFeignOrder8002 &#123;</span><br><span class="line">      public static void main(String[] args) &#123;</span><br><span class="line">          SpringApplication.run(OpenFeignOrder8002.class, args);</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>业务类</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>service</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>entities<span class="token punctuation">.</span></span><span class="token class-name">Payment</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>vo<span class="token punctuation">.</span></span><span class="token class-name">ResponseResult</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>openfeign<span class="token punctuation">.</span></span><span class="token class-name">FeignClient</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">GetMapping</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">PathVariable</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="11"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="12"></td><td><pre> * 2022/1/17</pre></td></tr><tr><td data-num="13"></td><td><pre> */</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token annotation punctuation">@FeignClient</span><span class="token punctuation">(</span><span class="token string">"CLOUD-PAYMENT-SERVICE"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">PaymentFeignService</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/payment/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token class-name">ResponseResult</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Payment</span><span class="token punctuation">></span></span> <span class="token function">getPaymentById</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">11</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>Feign 自带负载均衡配置项</p>
</li>
</ul>
<h3 id="openfeign超时控制"><a class="markdownIt-Anchor" href="#openfeign超时控制">#</a> OpenFeign 超时控制</h3>
<ul>
<li>
<p>OpenFeign 默认等待 1 秒钟，超过后报错</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">ribbon</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">ReadTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span></pre></td></tr><tr><td data-num="3"></td><td><pre>  <span class="token key atrule">ConnectTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span></pre></td></tr><tr><td data-num="4"></td><td><pre>&lt;<span class="token tag">!--code</span>￼12<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<blockquote>
<p>YML 文件里需要开启日志的 Feign 客户端</p>
</blockquote>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">logging:</span></span><br><span class="line">  <span class="attr">level:</span></span><br><span class="line">    <span class="comment">#Feign日志以什么级别监控哪个接口</span></span><br><span class="line">    <span class="attr">cn.wenhe9.springcloud.service.PaymentFeignService:</span> <span class="string">debug</span></span><br></pre></td></tr></table></figure></div>
<h2 id="hystrix断路器"><a class="markdownIt-Anchor" href="#hystrix断路器">#</a> Hystrix 断路器</h2>
<h3 id="概述-2"><a class="markdownIt-Anchor" href="#概述-2">#</a> 概述</h3>
<blockquote>
<p>分布式系统面临的问题</p>
</blockquote>
<ul>
<li>
<p><mark>复杂分布式体系结构中的应用程序有数十个依赖关系，每个依赖关系在某些时候将不可避免的失败</mark></p>
</li>
<li>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220117164451853.png"
                      alt="image-20220117164451853"
                ></p>
</li>
<li>
<p><mark>服务雪崩</mark></p>
</li>
<li>
<p>多个微服务之间调用的时候，假设微服务 A 调用微服务 B 和微服务 C，微服务 B 和微服务 C 有调用其他的微服务，这就是所谓的<mark>删除</mark>。如果删除的链路上某个微服务的响应时间过长或者不可用，对微服务 A 的调用就会占用越来越多的系统资源，进而引发系统崩溃，所谓的，“雪崩效应”</p>
</li>
<li>
<p>对于高流量的应用来说，单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和，鄙视白更糟糕的是，这些应用程序还可能导致服务之间的延迟增加，备份队列，线程和其他系统资源紧张，导致整个系统发生更多的级联故障，这些都表示需要对故障和延迟进行隔离和管理，以便单个依赖关系的失败，不能取消整个应用程序或系统</p>
</li>
<li>
<p>所以，通常当你发现一个模块下的某个实例失败后，这时候这个模块依然还会接收流量，然后这个有问题的模块还调用了其他的模块，这样就会发生级联故障，或者叫雪崩</p>
</li>
</ul>
<blockquote>
<p>Hystrix 是什么</p>
</blockquote>
<ul>
<li>Hystix 是一个用于处理分布式系统的延迟和容错的开源库，在分布式系统里，许多依赖不可避免的调用失败，比如超时、异常等，Hystrix 能够保证在一个依赖出现问题的情况下，<mark>不会导致整体服务失败，避免级联故障，以提供分布式系统的弹性</mark></li>
<li>&quot;断路器&quot; 本身是一种开关装置，当某个服务单元发生故障之后，通过断路器的故障监控（类似熔断保险丝），<mark>向调用方返回一个符合预期的、可处理的备选响应（FallBack），而不是长时间的等待或者抛出调用方法无法处理的异常</mark>，这样就保证了服务调用放的线程不会被长时间、不必要的占用，从而避免了故障在分布式系统中的蔓延，乃至雪崩</li>
</ul>
<blockquote>
<p>Hystrix 能干什么</p>
</blockquote>
<ul>
<li>服务降级</li>
<li>服务熔断</li>
<li>接近实时的监控</li>
<li>…</li>
</ul>
<h3 id="hystrix重要概念"><a class="markdownIt-Anchor" href="#hystrix重要概念">#</a> Hystrix 重要概念</h3>
<blockquote>
<p>服务降级 fallback</p>
</blockquote>
<ul>
<li>服务器繁忙，请稍后再试，不让客户端等待并立刻返回一个友好提示，fallback</li>
<li>哪些情况会触发降级
<ul>
<li>程序运行异常</li>
<li>超时</li>
<li>服务熔断触发服务降级</li>
<li>线程池 / 信号量打满也会导致服务降级</li>
</ul>
</li>
</ul>
<blockquote>
<p>服务熔断 break</p>
</blockquote>
<ul>
<li>类比保险丝达到最大访问后，直接拒绝访问，拉闸限电，然后调用服务降级的方法并返回友好提示</li>
<li>就是保险丝
<ul>
<li>服务的降级 -&gt; 进而熔断 -&gt; 恢复调用链路</li>
</ul>
</li>
</ul>
<blockquote>
<p>服务限流 flowlimit</p>
</blockquote>
<ul>
<li>秒杀高并发等操作，严禁一窝蜂的过来拥挤，大家排队，一秒钟 N 个，有序进行</li>
</ul>
<h3 id="服务降级"><a class="markdownIt-Anchor" href="#服务降级">#</a> 服务降级</h3>
<ul>
<li>
<p>使用注解的方式，服务端和客户端都可以采用，当使用注解方式时，会优先采用注解的方式的服务降级</p>
<ul>
<li>
<p>在主启动类上添加注解</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>netflix<span class="token punctuation">.</span>hystrix<span class="token punctuation">.</span></span><span class="token class-name">EnableHystrix</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>openfeign<span class="token punctuation">.</span></span><span class="token class-name">EnableFeignClients</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="9"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="10"></td><td><pre> * 2022/1/17</pre></td></tr><tr><td data-num="11"></td><td><pre> */</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token annotation punctuation">@EnableFeignClients</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token annotation punctuation">@EnableHystrix</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderHystrixMain8002</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">OrderHystrixMain8002</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="20"></td><td><pre></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">14</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>在类上面的全局服务降级</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">PaymentHystrixService</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>netflix<span class="token punctuation">.</span>hystrix<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>javanica<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">DefaultProperties</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>netflix<span class="token punctuation">.</span>hystrix<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>javanica<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">HystrixCommand</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>netflix<span class="token punctuation">.</span>hystrix<span class="token punctuation">.</span>contrib<span class="token punctuation">.</span>javanica<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">HystrixProperty</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span>extern<span class="token punctuation">.</span>slf4j<span class="token punctuation">.</span></span><span class="token class-name">Slf4j</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">GetMapping</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">PathVariable</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Resource</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="15"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="16"></td><td><pre> * 还不错，就是这个屏幕不大行</pre></td></tr><tr><td data-num="17"></td><td><pre> * 2022/1/17</pre></td></tr><tr><td data-num="18"></td><td><pre> */</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token annotation punctuation">@Slf4j</span></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token annotation punctuation">@DefaultProperties</span><span class="token punctuation">(</span></pre></td></tr><tr><td data-num="22"></td><td><pre>        defaultFallback <span class="token operator">=</span> <span class="token string">"paymentGlobalFallbackMethod"</span><span class="token punctuation">,</span></pre></td></tr><tr><td data-num="23"></td><td><pre>        commandProperties <span class="token operator">=</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="24"></td><td><pre>                <span class="token annotation punctuation">@HystrixProperty</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"execution.isolation.thread.timeoutInMilliseconds"</span><span class="token punctuation">,</span> value <span class="token operator">=</span> <span class="token string">"2000"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="25"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="26"></td><td><pre><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="27"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderHystrixController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="28"></td><td><pre>    <span class="token annotation punctuation">@Resource</span></pre></td></tr><tr><td data-num="29"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">PaymentHystrixService</span> paymentHystrixService<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="30"></td><td><pre></pre></td></tr><tr><td data-num="31"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/consumer/payment/hystrix/ok/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="32"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfoOk</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="33"></td><td><pre>        <span class="token class-name">String</span> result <span class="token operator">=</span> paymentHystrixService<span class="token punctuation">.</span><span class="token function">paymentInfoOk</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="34"></td><td><pre>        <span class="token keyword">return</span> result<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="35"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="36"></td><td><pre></pre></td></tr><tr><td data-num="37"></td><td><pre></pre></td></tr><tr><td data-num="38"></td><td><pre><span class="token comment">//    @HystrixCommand(</span></pre></td></tr><tr><td data-num="39"></td><td><pre><span class="token comment">//            fallbackMethod = "paymentInfoTimeoutHandler",</span></pre></td></tr><tr><td data-num="40"></td><td><pre><span class="token comment">//            commandProperties = &#123;</span></pre></td></tr><tr><td data-num="41"></td><td><pre><span class="token comment">//                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")</span></pre></td></tr><tr><td data-num="42"></td><td><pre><span class="token comment">//            &#125;</span></pre></td></tr><tr><td data-num="43"></td><td><pre><span class="token comment">//    )</span></pre></td></tr><tr><td data-num="44"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/payment/hystrix/timeout/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="45"></td><td><pre>    <span class="token annotation punctuation">@HystrixCommand</span></pre></td></tr><tr><td data-num="46"></td><td><pre>    <span class="token class-name">String</span> <span class="token function">paymentInfoTimeout</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="47"></td><td><pre>        <span class="token class-name">String</span> result <span class="token operator">=</span> paymentHystrixService<span class="token punctuation">.</span><span class="token function">paymentInfoTimeout</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="48"></td><td><pre>        <span class="token keyword">return</span> result<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="49"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="50"></td><td><pre></pre></td></tr><tr><td data-num="51"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfoTimeoutHandler</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="52"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"我是80，你错了！滚！"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="53"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="54"></td><td><pre></pre></td></tr><tr><td data-num="55"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentGlobalFallbackMethod</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="56"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"我是80，你错了！滚！越远越好！"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="57"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="58"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="59"></td><td><pre></pre></td></tr><tr><td data-num="60"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">15</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>PaymentFallbackService</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>service</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="6"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="7"></td><td><pre> * 2022/1/19</pre></td></tr><tr><td data-num="8"></td><td><pre> */</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token annotation punctuation">@Component</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PaymentFallbackService</span> <span class="token keyword">implements</span> <span class="token class-name">PaymentHystrixService</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfoOk</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"----- PaymentFallbackService fall back-paymentInfo_ok. 失败了"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="15"></td><td><pre></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">paymentInfoTimeout</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> id<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"----- PaymentFallbackService fall back-paymentInfo_ok. 失败了"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="21"></td><td><pre></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">16</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="服务熔断"><a class="markdownIt-Anchor" href="#服务熔断">#</a> 服务熔断</h3>
<blockquote>
<p>熔断是什么</p>
</blockquote>
<ul>
<li>熔断机制概述
<ul>
<li>熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务出错不可用或者响应时间太长时，会进行服务的降级，进而熔断该节点微服务的调用，快速返回错误的相应信息</li>
<li><mark>当检测到该节点的微服务调用相应正常后，恢复调用链路</mark></li>
</ul>
</li>
<li>在 Spring Cloud 框架里，熔断机制通过 Hystrix 实现，Hystrix 会监控微服务间的调用情况</li>
<li>当失败的调用到一定阈值时，缺省是 5 秒内 20 次调用失败，就会启动熔断机制，熔断机制的注解是 <code>@hystrixCommand</code></li>
</ul>
<p><mark>调用失败会触发降级，而降级会调用 fallback 方法，但无论如何降级的流程一定会先调用正常方法再调用 fallback 方法，假如单位时间内调用失败次数过多，也就是降级次数过多，则触发熔断机制，熔断以后就会跳过正常方法直接调用 fallback 方法，所谓 “熔断后服务不可用”，就是因为跳过了正常方法直接执行 fallback</mark></p>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220119230024082.png"
                      alt="image-20220119230024082"
                ></p>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220119230112586.png"
                      alt="image-20220119230112586"
                ></p>
<ul>
<li>当 I 秒的睡眠窗口时间结束，就创建一个新的包，其他的就会划过并被放弃</li>
</ul>
<blockquote>
<p>原来的主逻辑要如何恢复呢</p>
</blockquote>
<ul>
<li>对于这一问题，hystrix 为我们提供了自动恢复功能</li>
<li>当断路器打开，对主逻辑进行熔断之后，hystrix 会启动一个休眠时间窗，在这个时间窗内，降级逻辑是临时的称为主逻辑，当休眠时间窗到期，断路器将进入半开状态，释放一次请求到原来的主逻辑上，如果此次请求正常返回，那么断路器将继续闭合，主逻辑恢复，如果此次请求依然有问题，断路器继续进入打开状态，休眠时间窗重新计时</li>
</ul>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> cn.wenhe9.springcloud.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.util.IdUtil;</span><br><span class="line"><span class="keyword">import</span> com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;</span><br><span class="line"><span class="keyword">import</span> com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;</span><br><span class="line"><span class="keyword">import</span> com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cloud.commons.util.IdUtils;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Service;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.PathVariable;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.TimeUnit;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> DuJinliang</span></span><br><span class="line"><span class="comment"> * 2022/1/17</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PaymentService</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//服务降级</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 正常访问，肯定ok</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">paymentInfoOk</span><span class="params">(Integer id)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;线程 ： +&quot;</span> + Thread.currentThread().getName() + <span class="string">&quot; paymentInfoOk, id : &quot;</span> + id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 正常访问，肯定ok</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@HystrixCommand(</span></span><br><span class="line"><span class="meta">            fallbackMethod = &quot;paymentInfoTimeoutHandler&quot;,</span></span><br><span class="line"><span class="meta">            commandProperties = &#123;</span></span><br><span class="line"><span class="meta">                    @HystrixProperty(name = &quot;execution.isolation.thread.timeoutInMilliseconds&quot;, value = &quot;4000&quot;)</span></span><br><span class="line"><span class="meta">            &#125;</span></span><br><span class="line"><span class="meta">    )</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">paymentInfoTimeout</span><span class="params">(Integer id)</span>&#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">10</span> / <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            TimeUnit.SECONDS.sleep(<span class="number">3</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;线程 ： +&quot;</span> + Thread.currentThread().getName() + <span class="string">&quot; paymentInfoTimeout, id : &quot;</span> + id + <span class="string">&quot;耗时三秒钟&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">paymentInfoTimeoutHandler</span><span class="params">(Integer id)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;线程 ： +&quot;</span> + Thread.currentThread().getName() + <span class="string">&quot; paymentInfoTimeoutHandler, &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//===服务熔断</span></span><br><span class="line">    <span class="meta">@HystrixCommand(</span></span><br><span class="line"><span class="meta">        fallbackMethod = &quot;paymentCircuitBreakerFallback&quot;,</span></span><br><span class="line"><span class="meta">        commandProperties = &#123;</span></span><br><span class="line"><span class="meta">            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = &quot;true&quot;), //是否开启断路器</span></span><br><span class="line"><span class="meta">            @HystrixProperty(</span></span><br><span class="line"><span class="meta">                    name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,</span></span><br><span class="line"><span class="meta">                    value = &quot;10&quot;), //请求次数</span></span><br><span class="line"><span class="meta">            @HystrixProperty(</span></span><br><span class="line"><span class="meta">                    name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,</span></span><br><span class="line"><span class="meta">                    value = &quot;10000&quot;), //时间窗口期 / 时间范围</span></span><br><span class="line"><span class="meta">            @HystrixProperty(</span></span><br><span class="line"><span class="meta">                    name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,</span></span><br><span class="line"><span class="meta">                    value = &quot;60&quot;) //失败率达到多少后跳闸</span></span><br><span class="line"><span class="meta">        &#125;</span></span><br><span class="line"><span class="meta">    )</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">paymentCircuitBreaker</span><span class="params">(Integer id)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (id &lt; <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;*** id 不能为 负数&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">String</span> <span class="variable">serialNumber</span> <span class="operator">=</span> IdUtil.simpleUUID();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Thread.currentThread().getName() + <span class="string">&quot;\t调用成功，流水号 : &quot;</span> + serialNumber;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">paymentCircuitBreakerFallback</span><span class="params">(Integer id)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;id 不能为负数，请稍后再试 &quot;</span> + id;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure></div>
<h3 id="图形化监控"><a class="markdownIt-Anchor" href="#图形化监控">#</a> 图形化监控</h3>
<ul>
<li>
<p>建立一个单独的项目，导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-hystrix-dashboard<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-actuator<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="13"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 编写配置</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    server:</span><br><span class="line">      port: 9001</span><br><span class="line">    hystrix:</span><br><span class="line">      dashboard:</span><br><span class="line">        proxy-stream-allow-list: localhost</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>注解启动</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>netflix<span class="token punctuation">.</span>hystrix<span class="token punctuation">.</span>dashboard<span class="token punctuation">.</span></span><span class="token class-name">EnableHystrixDashboard</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="8"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="9"></td><td><pre> * 2022/1/20</pre></td></tr><tr><td data-num="10"></td><td><pre> */</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@EnableHystrixDashboard</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HystrixDashboard9001</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">HystrixDashboard9001</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">19</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h2 id="gateway服务网关"><a class="markdownIt-Anchor" href="#gateway服务网关">#</a> Gateway 服务网关</h2>
<h3 id="概述-3"><a class="markdownIt-Anchor" href="#概述-3">#</a> 概述</h3>
<blockquote>
<p>是什么？</p>
</blockquote>
<ul>
<li>Spring Cloud Gateway 使用的 Webflux 中的 reactor-netty 响应式编程组件，底层使用了 Netty 通讯框架</li>
<li>基于异步非阻塞模型进行开发的</li>
</ul>
<blockquote>
<p>能干什么？</p>
</blockquote>
<ul>
<li>反向代理</li>
<li>鉴权</li>
<li>流量控制</li>
<li>熔断</li>
<li>日志监控</li>
<li>…</li>
</ul>
<blockquote>
<p>微服务架构中网关在哪里</p>
</blockquote>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220121173321528.png"
                      alt="image-20220121173321528"
                ></p>
<blockquote>
<p>特性</p>
</blockquote>
<ul>
<li>基于 Spring Framework 5, Project Reactor 和 Spring Boo2.0 进行构建</li>
<li>动态路由：能够匹配任何请求属性</li>
<li>可以对路由指定 Predicate（断言）和 Filter（过滤器）</li>
<li>集成 Hystrix 的断路器功能</li>
<li>集成 Spring Cloud 服务发现功能</li>
<li>易于编写的 Predicate（断言）和 Filter（过滤器）</li>
<li>请求限流功能</li>
<li>支持路径重写</li>
</ul>
<h3 id="三大核心概念"><a class="markdownIt-Anchor" href="#三大核心概念">#</a> 三大核心概念</h3>
<blockquote>
<p>Route（路由）</p>
</blockquote>
<ul>
<li>路由是构建网关的基本模块，他由 ID，目标 URI，一系列的断言和过滤器组成，如果断言为 true 则匹配该路由</li>
</ul>
<blockquote>
<p>Predicate（断言）</p>
</blockquote>
<ul>
<li>参考的是 Java8 的 java.util.function.Predicate</li>
<li>开发人员可以匹配 HTTP 请求中的所有内容（例如请求头或请求参数），如果请求与断言相匹配则进行路由</li>
</ul>
<blockquote>
<p>Filter（过滤）</p>
</blockquote>
<ul>
<li>指的是 Spring 框架中 GatewayFilter 的实例，使用过滤器，可以在请求路由前或者之后对请求进行修改</li>
</ul>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220121190645557.png"
                      alt="image-20220121190645557"
                ></p>
<h3 id="gateway工作流程"><a class="markdownIt-Anchor" href="#gateway工作流程">#</a> Gateway 工作流程</h3>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220121191209732.png"
                      alt="Spring Cloud Gateway Diagram"
                ></p>
<ul>
<li>客户端向 Spring Cloud Gateway 发出请求，然后在 <code>Gateway Handler Mapping</code>  中找到与请求相匹配的路由，将其发送到 <code>Gateway Web Handler</code></li>
<li>Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑，然后返回</li>
<li>过滤器之间用虚线分开是因为过滤器可能在发送代理请求之前（“pre”）或之后（“post”）执行业务逻辑</li>
<li>Filter 在 &quot;pre&quot; 类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等，在 &quot;post&quot; 类型的过滤器中可以做响应内容、响应头的修改，日志的输出，流量监控等有着非常重要的作用</li>
</ul>
<p><mark>核心逻辑</mark> : <mark>路由转发 + 执行过滤器链</mark></p>
<h3 id="入门配置"><a class="markdownIt-Anchor" href="#入门配置">#</a> 入门配置</h3>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-gateway<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 在启动类上声明注解</span><br><span class="line"></span><br><span class="line">  - ```java</span><br><span class="line">    package cn.wenhe9.springcloud;</span><br><span class="line">    </span><br><span class="line">    import org.springframework.boot.SpringApplication;</span><br><span class="line">    import org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line">    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;</span><br><span class="line">    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;</span><br><span class="line">    </span><br><span class="line">    /**</span><br><span class="line">     * @author DuJinliang</span><br><span class="line">     * 2022/1/21</span><br><span class="line">     */</span><br><span class="line">    @SpringBootApplication</span><br><span class="line">    @EnableEurekaClient</span><br><span class="line">    public class GatewayMain9627 &#123;</span><br><span class="line">        public static void main(String[] args) &#123;</span><br><span class="line">            SpringApplication.run(GatewayMain9627.class, args);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>编写配置，方式一</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9627</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">loadbalancer</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">ribbon</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token key atrule">gateway</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>      <span class="token key atrule">routes</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> payment_routh <span class="token comment"># 路由的 id，没有固定规则但要求唯一，建议配合服务名</span></pre></td></tr><tr><td data-num="13"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8001</span> <span class="token comment"># 匹配后提供服务的路由地址</span></pre></td></tr><tr><td data-num="14"></td><td><pre>          <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="15"></td><td><pre>            <span class="token punctuation">-</span> Path=/payment/<span class="token important">**</span> <span class="token comment"># 断言，路径相匹配的进行路由</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="17"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token key atrule">hostname</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway<span class="token punctuation">-</span>service</pre></td></tr><tr><td data-num="19"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="21"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="23"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="24"></td><td><pre>&lt;<span class="token tag">!--code</span>￼21<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="通过微服务名实现动态路由"><a class="markdownIt-Anchor" href="#通过微服务名实现动态路由">#</a> 通过微服务名实现动态路由</h3>
<ul>
<li>
<p>默认情况下，Gateway 会根据注册中心注册的服务列表，以注册中心上微服务名为路径创建动态路由进行转发，从而实现动态路由的功</p>
</li>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9627</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">loadbalancer</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">ribbon</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token key atrule">gateway</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token key atrule">locator</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>          <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> <span class="token comment"># 开启从注册中心动态创建路由的功能，利用微服务名进行路由</span></pre></td></tr><tr><td data-num="14"></td><td><pre>      <span class="token key atrule">routes</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> payment_routh <span class="token comment"># 路由的 id，没有固定规则但要求唯一，建议配合服务名</span></pre></td></tr><tr><td data-num="16"></td><td><pre>          <span class="token comment"># uri: http://localhost:8001 # 匹配后提供服务的路由地址</span></pre></td></tr><tr><td data-num="17"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//CLOUD<span class="token punctuation">-</span>PAYMENT<span class="token punctuation">-</span>SERVICE</pre></td></tr><tr><td data-num="18"></td><td><pre>          <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="19"></td><td><pre>            <span class="token punctuation">-</span> Path=/payment/<span class="token important">**</span> <span class="token comment"># 断言，路径相匹配的进行路由</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="21"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token key atrule">hostname</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway<span class="token punctuation">-</span>service</pre></td></tr><tr><td data-num="23"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="25"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="26"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="27"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="28"></td><td><pre>&lt;<span class="token tag">!--code</span>￼22<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
<h3 id="filter的使用"><a class="markdownIt-Anchor" href="#filter的使用">#</a> Filter 的使用</h3>
<blockquote>
<p>是什么</p>
</blockquote>
<ul>
<li>路由过滤器可用于修改进入的 HTTP 请求和返回的 HTTP 响应，路由过滤器只能指定路由进行使用</li>
<li>Spring Cloud  Gateway 内置了多种路由过滤器，他们都由 GatewayFilter 的工厂类产生</li>
</ul>
<blockquote>
<p>Spring Cloud Gateway 的 Filter</p>
</blockquote>
<ul>
<li>
<p>声明周期</p>
<ul>
<li>pre 业务逻辑之前</li>
<li>post  业务逻辑之后</li>
</ul>
</li>
<li>
<p>种类</p>
<ul>
<li>GatewayFilter</li>
<li>GlobalFilter</li>
<li>看官网</li>
</ul>
</li>
</ul>
<blockquote>
<p>自定义过滤器</p>
</blockquote>
<ul>
<li>
<p>自定义全局 GlobalFilter</p>
<ul>
<li>两个主要接口
<ul>
<li>GlobalFilter,Ordered</li>
</ul>
</li>
</ul>
</li>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>filter</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span>extern<span class="token punctuation">.</span>slf4j<span class="token punctuation">.</span></span><span class="token class-name">Slf4j</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span></span><span class="token class-name">GatewayFilterChain</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span></span><span class="token class-name">GlobalFilter</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>core<span class="token punctuation">.</span></span><span class="token class-name">Ordered</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpStatus</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>server<span class="token punctuation">.</span></span><span class="token class-name">ServerWebExchange</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">reactor<span class="token punctuation">.</span>core<span class="token punctuation">.</span>publisher<span class="token punctuation">.</span></span><span class="token class-name">Mono</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Date</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="15"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="16"></td><td><pre> * 2022/1/21</pre></td></tr><tr><td data-num="17"></td><td><pre> */</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token annotation punctuation">@Component</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token annotation punctuation">@Slf4j</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyLogGateWayFilter</span> <span class="token keyword">implements</span> <span class="token class-name">GlobalFilter</span><span class="token punctuation">,</span> <span class="token class-name">Ordered</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="21"></td><td><pre></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="23"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">></span></span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> exchange<span class="token punctuation">,</span> <span class="token class-name">GatewayFilterChain</span> chain<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="24"></td><td><pre>        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"******come in MylogGatewayFilter: &#123;&#125;"</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="25"></td><td><pre>        <span class="token class-name">String</span> username <span class="token operator">=</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getQueryParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getFirst</span><span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="26"></td><td><pre>        <span class="token keyword">if</span> <span class="token punctuation">(</span>username <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="27"></td><td><pre>            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"******用户名为null，非法用户"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="28"></td><td><pre>            exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setStatusCode</span><span class="token punctuation">(</span><span class="token class-name">HttpStatus</span><span class="token punctuation">.</span><span class="token constant">NOT_ACCEPTABLE</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="29"></td><td><pre>            <span class="token keyword">return</span> exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setComplete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="30"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="31"></td><td><pre>        <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="32"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="33"></td><td><pre></pre></td></tr><tr><td data-num="34"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="35"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="36"></td><td><pre>        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="37"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="38"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="39"></td><td><pre></pre></td></tr><tr><td data-num="40"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">23</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3344</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>config<span class="token punctuation">-</span>center</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">git</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//gitee.com/du<span class="token punctuation">-</span>jinliang/springcloud<span class="token punctuation">-</span>config.git</pre></td></tr><tr><td data-num="11"></td><td><pre>          <span class="token key atrule">search-paths</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>            <span class="token punctuation">-</span> springcloud<span class="token punctuation">-</span>config</pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token key atrule">label</span><span class="token punctuation">:</span> master</pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="15"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="18"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="19"></td><td><pre>&lt;<span class="token tag">!--code</span>￼24<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="config客户端配置与测试"><a class="markdownIt-Anchor" href="#config客户端配置与测试">#</a> Config 客户端配置与测试</h3>
<blockquote>
<p>bootstrap.yml</p>
</blockquote>
<ul>
<li>是什么？
<ul>
<li>application.yml 是用户级的资源配置项</li>
<li>bootstrap.yml 是系统级的，优先级更高</li>
</ul>
</li>
<li>Spring Cloud 会创建一个 “Bootstap Context”，作为 Spring 应用的 <code>Application Context</code>  的父上下文。初始化的时候， <code>Bootstrap Context</code>  负责从<mark>外部源</mark>加载配置属性并解析配置。这两个上下文共享一个从外部获取的 <code>Environment</code></li>
<li><code>Boostrap</code>  属性有高优先级，默认情况下，他们不会被本地配置覆盖， <code>Bootstrap Context</code>  和  <code>Application Context</code>  有着不同的约定，所以新增了一个 <code>bootstrap.yml</code> ，保证 <code>Bootstrap Context</code>  和 <code>Application Context</code>  配置的分离</li>
</ul>
<p><mark>要将 client 模块下的 application.yml 文件改为 bootstrap.yml，这是很关键的</mark></p>
<p>因为 <code>bootstrap.yml</code>  是比 <code>application.yml</code>  先加载的。 <code>bootstrap.yml</code>  优先级高于 <code>application.yml</code></p>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-config<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 编写配置</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    server:</span><br><span class="line">      port: 3355</span><br><span class="line">    spring:</span><br><span class="line">      application:</span><br><span class="line">        name: config-client</span><br><span class="line">      cloud:</span><br><span class="line">        # config客户端配置</span><br><span class="line">        config:</span><br><span class="line">          label: master # 分支名称</span><br><span class="line">          name: config # 配置文件名称</span><br><span class="line">          profile: dev # 读取后缀名称 上述是三个综合：master分支上的config-dev.yml的配置文件被读取</span><br><span class="line">          uri: http://localhost:3344 # 配置中心地址</span><br><span class="line">    eureka:</span><br><span class="line">      client:</span><br><span class="line">        register-with-eureka: false</span><br><span class="line">        service-url:</span><br><span class="line">          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>注解声明启动类</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">jdk<span class="token punctuation">.</span>nashorn<span class="token punctuation">.</span>internal<span class="token punctuation">.</span>runtime<span class="token punctuation">.</span>regexp<span class="token punctuation">.</span>joni<span class="token punctuation">.</span></span><span class="token class-name">Config</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>netflix<span class="token punctuation">.</span>eureka<span class="token punctuation">.</span></span><span class="token class-name">EnableEurekaClient</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="9"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="10"></td><td><pre> * 2022/1/22</pre></td></tr><tr><td data-num="11"></td><td><pre> */</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token annotation punctuation">@EnableEurekaClient</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfigClientMain3355</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">ConfigClientMain3355</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">26</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">management</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">endpoints</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">web</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>      <span class="token key atrule">exposure</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        <span class="token key atrule">include</span><span class="token punctuation">:</span> <span class="token string">"*"</span></pre></td></tr><tr><td data-num="6"></td><td><pre>&lt;<span class="token tag">!--code</span>￼27<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3344</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>config<span class="token punctuation">-</span>center</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">git</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//gitee.com/du<span class="token punctuation">-</span>jinliang/springcloud<span class="token punctuation">-</span>config.git</pre></td></tr><tr><td data-num="11"></td><td><pre>          <span class="token key atrule">search-paths</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>            <span class="token punctuation">-</span> springcloud<span class="token punctuation">-</span>config</pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token key atrule">label</span><span class="token punctuation">:</span> master</pre></td></tr><tr><td data-num="14"></td><td><pre>  <span class="token comment"># rabbitmq 相关配置</span></pre></td></tr><tr><td data-num="15"></td><td><pre>  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token key atrule">host</span><span class="token punctuation">:</span> 39.106.88.176</pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">5672</span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token key atrule">username</span><span class="token punctuation">:</span> meteor</pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token string">"0209"</span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token key atrule">virtual-host</span><span class="token punctuation">:</span> wenhe9</pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="22"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="23"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="25"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="26"></td><td><pre><span class="token comment"># rabbitmq 相关配置，暴露 bus 刷新配置的端点</span></pre></td></tr><tr><td data-num="27"></td><td><pre><span class="token key atrule">management</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="28"></td><td><pre>  <span class="token key atrule">endpoints</span><span class="token punctuation">:</span> <span class="token comment"># 暴露 bus 刷新配置的端点</span></pre></td></tr><tr><td data-num="29"></td><td><pre>    <span class="token key atrule">web</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="30"></td><td><pre>      <span class="token key atrule">exposure</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="31"></td><td><pre>        <span class="token key atrule">include</span><span class="token punctuation">:</span> <span class="token string">"bus-refresh"</span></pre></td></tr><tr><td data-num="32"></td><td><pre>&lt;<span class="token tag">!--code</span>￼28<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3355</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> config<span class="token punctuation">-</span>client</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token comment"># config 客户端配置</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>      <span class="token key atrule">label</span><span class="token punctuation">:</span> master <span class="token comment"># 分支名称</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">name</span><span class="token punctuation">:</span> config <span class="token comment"># 配置文件名称</span></pre></td></tr><tr><td data-num="11"></td><td><pre>      <span class="token key atrule">profile</span><span class="token punctuation">:</span> dev <span class="token comment"># 读取后缀名称 上述是三个综合：master 分支上的 config-dev.yml 的配置文件被读取</span></pre></td></tr><tr><td data-num="12"></td><td><pre>      <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">3344</span> <span class="token comment"># 配置中心地址</span></pre></td></tr><tr><td data-num="13"></td><td><pre>  <span class="token comment"># rabbitmq 相关配置</span></pre></td></tr><tr><td data-num="14"></td><td><pre>  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token key atrule">host</span><span class="token punctuation">:</span> 39.106.88.176</pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">5672</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token key atrule">username</span><span class="token punctuation">:</span> meteor</pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token string">"0209"</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token key atrule">virtual-host</span><span class="token punctuation">:</span> wenhe9</pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="21"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="23"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="24"></td><td><pre><span class="token key atrule">management</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="25"></td><td><pre>  <span class="token key atrule">endpoints</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="26"></td><td><pre>    <span class="token key atrule">web</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="27"></td><td><pre>      <span class="token key atrule">exposure</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="28"></td><td><pre>        <span class="token key atrule">include</span><span class="token punctuation">:</span> <span class="token string">"*"</span></pre></td></tr><tr><td data-num="29"></td><td><pre>&lt;<span class="token tag">!--code</span>￼29<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<p>当需要定点刷新时，需附带目标，目标由应用名称和端口号组成</p>
<ul>
<li>
<div class="highlight-container" data-rel="Shell"><figure class="iseeu highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">curl</span> <span class="token parameter variable">-X</span> POST <span class="token string">"http://localhost:3344/actuator/bus-refresh/&#123;destination&#125;"</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token function">curl</span> <span class="token parameter variable">-X</span> POST <span class="token string">"http://localhsot:3344/actuator/bus-refresh/config-client:3355"</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span>--code￼30--<span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8802</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>stream<span class="token punctuation">-</span>consuemr</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">host</span><span class="token punctuation">:</span> 39.106.88.176</pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token key atrule">virtual-host</span><span class="token punctuation">:</span> wenhe9</pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token key atrule">username</span><span class="token punctuation">:</span> meteor</pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token string">"0209"</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">5672</span></pre></td></tr><tr><td data-num="12"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>    <span class="token key atrule">stream</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre>      <span class="token key atrule">binders</span><span class="token punctuation">:</span> <span class="token comment"># 在此处配置与绑定的 rabbitmq 的消息服务</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token key atrule">defaultRabbit</span><span class="token punctuation">:</span> <span class="token comment"># 定义的名称，用于 binding 整合</span></pre></td></tr><tr><td data-num="16"></td><td><pre>          <span class="token key atrule">type</span><span class="token punctuation">:</span> rabbit</pre></td></tr><tr><td data-num="17"></td><td><pre>          <span class="token key atrule">environment</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="18"></td><td><pre>            <span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="19"></td><td><pre>              <span class="token key atrule">rabbitmq</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="20"></td><td><pre>                <span class="token key atrule">host</span><span class="token punctuation">:</span> 39.106.88.176</pre></td></tr><tr><td data-num="21"></td><td><pre>                <span class="token key atrule">virtual-host</span><span class="token punctuation">:</span> wenhe9</pre></td></tr><tr><td data-num="22"></td><td><pre>                <span class="token key atrule">username</span><span class="token punctuation">:</span> meteor</pre></td></tr><tr><td data-num="23"></td><td><pre>                <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token string">"0209"</span></pre></td></tr><tr><td data-num="24"></td><td><pre>                <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">5672</span></pre></td></tr><tr><td data-num="25"></td><td><pre>      <span class="token key atrule">bindings</span><span class="token punctuation">:</span> <span class="token comment"># 服务的整合处理</span></pre></td></tr><tr><td data-num="26"></td><td><pre>        <span class="token key atrule">input</span><span class="token punctuation">:</span> <span class="token comment"># 这个名字是一个通道的名称</span></pre></td></tr><tr><td data-num="27"></td><td><pre>          <span class="token key atrule">destination</span><span class="token punctuation">:</span> studyExchange <span class="token comment"># 表示用使用的 exchange 名称定义</span></pre></td></tr><tr><td data-num="28"></td><td><pre>          <span class="token key atrule">content-type</span><span class="token punctuation">:</span> application/json <span class="token comment"># 设置消息类型，本次为 json 对象，如果是文本则设置 text/plain</span></pre></td></tr><tr><td data-num="29"></td><td><pre>          <span class="token key atrule">binder</span><span class="token punctuation">:</span> <span class="token punctuation">&#123;</span> defaultRabbit <span class="token punctuation">&#125;</span> <span class="token comment"># 设置要绑定的消息服务的具体配置</span></pre></td></tr><tr><td data-num="30"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="31"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="32"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="33"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="34"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="35"></td><td><pre>    <span class="token key atrule">lease-renewal-interval-in-seconds</span><span class="token punctuation">:</span> <span class="token number">2</span> <span class="token comment"># 设置心跳的时间间隔（默认是 30 秒）</span></pre></td></tr><tr><td data-num="36"></td><td><pre>    <span class="token key atrule">lease-expiration-duration-in-seconds</span><span class="token punctuation">:</span> <span class="token number">5</span> <span class="token comment"># 失去心跳的最久时间</span></pre></td></tr><tr><td data-num="37"></td><td><pre>    <span class="token key atrule">instance-id</span><span class="token punctuation">:</span> send<span class="token punctuation">-</span>8802.com <span class="token comment"># 在信息列表时显示主机名称</span></pre></td></tr><tr><td data-num="38"></td><td><pre>    <span class="token key atrule">prefer-ip-address</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> <span class="token comment"># 访问的路径变为 IP 地址</span></pre></td></tr><tr><td data-num="39"></td><td><pre>&lt;<span class="token tag">!--code</span>￼31<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>service<span class="token punctuation">.</span>impl</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">IMessageProvider</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span>extern<span class="token punctuation">.</span>slf4j<span class="token punctuation">.</span></span><span class="token class-name">Slf4j</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>stream<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">EnableBinding</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>stream<span class="token punctuation">.</span>messaging<span class="token punctuation">.</span></span><span class="token class-name">Source</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>messaging<span class="token punctuation">.</span></span><span class="token class-name">Message</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>messaging<span class="token punctuation">.</span></span><span class="token class-name">MessageChannel</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>messaging<span class="token punctuation">.</span>support<span class="token punctuation">.</span></span><span class="token class-name">MessageBuilder</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Service</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Resource</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">UUID</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="16"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="17"></td><td><pre> * 2022/1/25</pre></td></tr><tr><td data-num="18"></td><td><pre> * 定义消息的推送管道</pre></td></tr><tr><td data-num="19"></td><td><pre> */</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token annotation punctuation">@EnableBinding</span><span class="token punctuation">(</span><span class="token class-name">Source</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token annotation punctuation">@Slf4j</span></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">IMessageProviderImpl</span> <span class="token keyword">implements</span> <span class="token class-name">IMessageProvider</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="23"></td><td><pre></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token comment">/**</pre></td></tr><tr><td data-num="25"></td><td><pre>     * 消息发送管道</pre></td></tr><tr><td data-num="26"></td><td><pre>     */</span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token annotation punctuation">@Resource</span></pre></td></tr><tr><td data-num="28"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">MessageChannel</span> output<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="29"></td><td><pre></pre></td></tr><tr><td data-num="30"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="31"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="32"></td><td><pre>        <span class="token class-name">String</span> serial <span class="token operator">=</span> <span class="token constant">UUID</span><span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="33"></td><td><pre>        <span class="token class-name">Message</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> message <span class="token operator">=</span> <span class="token class-name">MessageBuilder</span><span class="token punctuation">.</span><span class="token function">withPayload</span><span class="token punctuation">(</span>serial<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="34"></td><td><pre>        output<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="35"></td><td><pre>        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"****serial&#123;&#125;"</span><span class="token punctuation">,</span> serial<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="36"></td><td><pre>        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="37"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="38"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="39"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">32</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="消息驱动之消费者"><a class="markdownIt-Anchor" href="#消息驱动之消费者">#</a> 消息驱动之消费者</h3>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-stream-rabbit<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 编写配置</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    server:</span><br><span class="line">      port: 8802</span><br><span class="line">    spring:</span><br><span class="line">      application:</span><br><span class="line">        name: cloud-stream-consuemr</span><br><span class="line">      rabbitmq:</span><br><span class="line">        host: 39.106.88.176</span><br><span class="line">        virtual-host: wenhe9</span><br><span class="line">        username: meteor</span><br><span class="line">        password: &quot;0209&quot;</span><br><span class="line">        port: 5672</span><br><span class="line">      cloud:</span><br><span class="line">        stream:</span><br><span class="line">          binders: # 在此处配置与绑定的rabbitmq的消息服务</span><br><span class="line">            defaultRabbit: # 定义的名称，用于binding整合</span><br><span class="line">              type: rabbit</span><br><span class="line">              environment:</span><br><span class="line">                spring:</span><br><span class="line">                  rabbitmq:</span><br><span class="line">                    host: 39.106.88.176</span><br><span class="line">                    virtual-host: wenhe9</span><br><span class="line">                    username: meteor</span><br><span class="line">                    password: &quot;0209&quot;</span><br><span class="line">                    port: 5672</span><br><span class="line">          bindings: # 服务的整合处理</span><br><span class="line">            input: # 这个名字是一个通道的名称</span><br><span class="line">              destination: studyExchange # 表示用使用的exchange名称定义</span><br><span class="line">              content-type: application/json # 设置消息类型，本次为json对象，如果是文本则设置text/plain</span><br><span class="line">              binder: &#123; defaultRabbit &#125; # 设置要绑定的消息服务的具体配置</span><br><span class="line">    eureka:</span><br><span class="line">      client:</span><br><span class="line">        service-url:</span><br><span class="line">          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka</span><br><span class="line">      instance:</span><br><span class="line">        lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔（默认是30秒）</span><br><span class="line">        lease-expiration-duration-in-seconds: 5 # 失去心跳的最久时间</span><br><span class="line">        instance-id: send-8802.com # 在信息列表时显示主机名称</span><br><span class="line">        prefer-ip-address: true # 访问的路径变为IP地址</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
<li>
<p>编写代码</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Value</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>stream<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">EnableBinding</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>stream<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">StreamListener</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>stream<span class="token punctuation">.</span>messaging<span class="token punctuation">.</span></span><span class="token class-name">Sink</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>messaging<span class="token punctuation">.</span></span><span class="token class-name">Message</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="12"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="13"></td><td><pre> * 2022/1/25</pre></td></tr><tr><td data-num="14"></td><td><pre> */</span></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token annotation punctuation">@Component</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token annotation punctuation">@EnableBinding</span><span class="token punctuation">(</span><span class="token class-name">Sink</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ReceiveMessageListenerController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;server.port&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">String</span> serverPort<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="20"></td><td><pre></pre></td></tr><tr><td data-num="21"></td><td><pre>    <span class="token annotation punctuation">@StreamListener</span><span class="token punctuation">(</span><span class="token class-name">Sink</span><span class="token punctuation">.</span><span class="token constant">INPUT</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">input</span><span class="token punctuation">(</span><span class="token class-name">Message</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">></span></span> message<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="23"></td><td><pre>        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"消费者1号， -----> 接收到的消息： "</span> <span class="token operator">+</span> message<span class="token punctuation">.</span><span class="token function">getPayload</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"\t port : "</span> <span class="token operator">+</span> serverPort<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="25"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="26"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">34</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8003</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>payment<span class="token punctuation">-</span>service</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">zipkin</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">base-url</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//39.106.88.176<span class="token punctuation">:</span><span class="token number">9411</span></pre></td></tr><tr><td data-num="8"></td><td><pre>  <span class="token key atrule">sleuth</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token key atrule">sampler</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">probability</span><span class="token punctuation">:</span> <span class="token number">1</span> <span class="token comment"># 采样率值介于 0 到 1 之间，1 则表示全部采集</span></pre></td></tr><tr><td data-num="11"></td><td><pre>  <span class="token key atrule">datasource</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token key atrule">type</span><span class="token punctuation">:</span> com.alibaba.druid.pool.DruidDataSource</pre></td></tr><tr><td data-num="13"></td><td><pre>    <span class="token key atrule">driver-class-name</span><span class="token punctuation">:</span> com.mysql.cj.jdbc.Driver</pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span>3306/cloudtest<span class="token punctuation">?</span>useUnicode=true<span class="token important">&amp;characterEncoding=utf-8&amp;useSSL=false</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token key atrule">username</span><span class="token punctuation">:</span> root</pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token string">"0209"</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token key atrule">mybatis</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="18"></td><td><pre>  <span class="token key atrule">mapper-locations</span><span class="token punctuation">:</span> classpath<span class="token punctuation">:</span>mapper/<span class="token important">*.xml</span></pre></td></tr><tr><td data-num="19"></td><td><pre>  <span class="token key atrule">type-aliases-package</span><span class="token punctuation">:</span> cn.wenhe9.springcloud.entities</pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="21"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token key atrule">instance-id</span><span class="token punctuation">:</span> payment8003</pre></td></tr><tr><td data-num="23"></td><td><pre>    <span class="token key atrule">prefer-ip-address</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="24"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="25"></td><td><pre>    <span class="token comment"># 表示是否将自己注册到 EurekaServer 默认为 true</span></pre></td></tr><tr><td data-num="26"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="27"></td><td><pre>    <span class="token comment"># 是否从 EurekaServer 抓取自己的注册信息，默认为 true，单节点无所谓，集群必须配置为 true 才能配合 ribbon 使用负载均衡</span></pre></td></tr><tr><td data-num="28"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="29"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="30"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka<span class="token punctuation">,</span>http<span class="token punctuation">:</span>//eureka7002.com<span class="token punctuation">:</span>7002/eureka</pre></td></tr><tr><td data-num="31"></td><td><pre>&lt;<span class="token tag">!--code</span>￼35<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9002</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>alibaba<span class="token punctuation">-</span>provider<span class="token punctuation">-</span>payment</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//39.106.88.176<span class="token punctuation">:</span>8848/</pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token key atrule">management</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>  <span class="token key atrule">endpoints</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token key atrule">web</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token key atrule">exposure</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token key atrule">include</span><span class="token punctuation">:</span> <span class="token string">"*"</span></pre></td></tr><tr><td data-num="15"></td><td><pre>&lt;<span class="token tag">!--code</span>￼36<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Value</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">GetMapping</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">PathVariable</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="9"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="10"></td><td><pre> * 2022/1/26</pre></td></tr><tr><td data-num="11"></td><td><pre> */</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PaymentController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;server.port&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">String</span> serverPort<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/payment/nacos/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getPayment</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="19"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"Nacos registry , serverPort : "</span> <span class="token operator">+</span> serverPort  <span class="token operator">+</span> <span class="token string">"\t id"</span> <span class="token operator">+</span>id<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">37</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8003</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>comsumer<span class="token punctuation">-</span>order</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//39.106.88.176<span class="token punctuation">:</span>8848/</pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token key atrule">ribbon</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>  <span class="token key atrule">ReadTimeout</span><span class="token punctuation">:</span> <span class="token number">3000</span></pre></td></tr><tr><td data-num="12"></td><td><pre>  <span class="token key atrule">ConnectionTimeout</span><span class="token punctuation">:</span> <span class="token number">3000</span></pre></td></tr><tr><td data-num="13"></td><td><pre>  </pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token key atrule">logging</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="15"></td><td><pre>  <span class="token key atrule">level</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token key atrule">cn.wenhe9.springcloud.service.PaymentFeignService</span><span class="token punctuation">:</span> info</pre></td></tr><tr><td data-num="17"></td><td><pre></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token comment"># 消费者将要去访问的微服务名称（注册成功进 nacos 的微服务提供者）</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="20"></td><td><pre>  <span class="token key atrule">nacos-user-service</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//nacos<span class="token punctuation">-</span>payment<span class="token punctuation">-</span>provider</pre></td></tr><tr><td data-num="21"></td><td><pre>&lt;<span class="token tag">!--code</span>￼38<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>config</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>hutool<span class="token punctuation">.</span>log<span class="token punctuation">.</span></span><span class="token class-name">Log</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">feign<span class="token punctuation">.</span></span><span class="token class-name">Logger</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Bean</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Configuration</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="9"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="10"></td><td><pre> * 2022/1/27</pre></td></tr><tr><td data-num="11"></td><td><pre> */</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@Configuration</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FeignConfig</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token annotation punctuation">@Bean</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token class-name">Logger<span class="token punctuation">.</span>Level</span> <span class="token function">feignLoggerLevel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token keyword">return</span> <span class="token class-name">Logger<span class="token punctuation">.</span>Level</span><span class="token punctuation">.</span><span class="token constant">BASIC</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">39</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">PaymentFeignService</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Value</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">GetMapping</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">PathVariable</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>client<span class="token punctuation">.</span></span><span class="token class-name">RestTemplate</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Resource</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="13"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="14"></td><td><pre> * 2022/1/27</pre></td></tr><tr><td data-num="15"></td><td><pre> */</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderNacosController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token annotation punctuation">@Resource</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="20"></td><td><pre></pre></td></tr><tr><td data-num="21"></td><td><pre>    <span class="token annotation punctuation">@Resource</span></pre></td></tr><tr><td data-num="22"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">PaymentFeignService</span> paymentFeignService<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="23"></td><td><pre></pre></td></tr><tr><td data-num="24"></td><td><pre>    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;service-url.nacos-user-service&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="25"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">String</span> serviceUrl<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="26"></td><td><pre></pre></td></tr><tr><td data-num="27"></td><td><pre><span class="token comment">//    @GetMapping("/order/payment/nacos/&#123;id&#125;")</span></pre></td></tr><tr><td data-num="28"></td><td><pre><span class="token comment">//    public String getPayment(@PathVariable(name = "id") Integer id)&#123;</span></pre></td></tr><tr><td data-num="29"></td><td><pre><span class="token comment">//        return restTemplate.getForObject(serviceUrl + "/payment/nacos/" + id, String.class);</span></pre></td></tr><tr><td data-num="30"></td><td><pre><span class="token comment">//    &#125;</span></pre></td></tr><tr><td data-num="31"></td><td><pre></pre></td></tr><tr><td data-num="32"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/order/payment/nacos/&#123;id&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="33"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getPayment</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">"id"</span><span class="token punctuation">)</span> <span class="token class-name">Integer</span> id<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="34"></td><td><pre>        <span class="token keyword">return</span> paymentFeignService<span class="token punctuation">.</span><span class="token function">getPayment</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="35"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="36"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="37"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">40</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h4 id="服务注册中心比较"><a class="markdownIt-Anchor" href="#服务注册中心比较">#</a> 服务注册中心比较</h4>
<ul>
<li>nacos 同时支持 AP 和 CP</li>
</ul>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220127114926407.png"
                      alt="image-20220127114926407"
                ></p>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220127115026032.png"
                      alt="image-20220127115026032"
                ></p>
<blockquote>
<p>Nacos 支持 AP 和 CP 模式的切换</p>
</blockquote>
<p><mark>C 是所有节点在同一时间看到的数据是一致的。而 A 的定义是所有的请求都会收到响应</mark></p>
<blockquote>
<p>何时选择何种模式</p>
</blockquote>
<ul>
<li>
<p>一般来说，如果不需要存储服务级别的信息且服务实例是通过 nacos-client 注册，并能够保持心跳上报，那么可以选择 AP 模式。当前主流的服务如 Spring Cloud 和 Dubbo 服务，都适用于 AP 模式，AP 模式为了服务的可用性而减弱了一致性，因此 AP 模式下只支持注册临时实例</p>
</li>
<li>
<p>如果需要在服务级别编辑或者存储配置信息，那么 CP 是必须的，K8S 服务和 DNS 服务则适用于 CP 模式。CP 模式下则支持注册持久化实例，此时则是以 Raft 协议为集群运行模式，该模式下注册实例之前必须先注册服务，如果服务不存在，则会返回错误。</p>
</li>
<li>
<div class="highlight-container" data-rel="Shell"><figure class="iseeu highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">curl</span> <span class="token parameter variable">-X</span> PUT <span class="token string">"<span class="token variable">$NACOS_SERVER</span>:8848/nacos/v1/ns/operator/switches?entry=serverMode&amp;value=CP"</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span>--code￼41--<span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>编写配置</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">3377</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> nacos<span class="token punctuation">-</span>config<span class="token punctuation">-</span>client</pre></td></tr><tr><td data-num="6"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 39.106.88.176<span class="token punctuation">:</span><span class="token number">8848</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 39.106.88.176<span class="token punctuation">:</span><span class="token number">8848</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yaml</pre></td></tr><tr><td data-num="13"></td><td><pre>  <span class="token key atrule">profiles</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token key atrule">active</span><span class="token punctuation">:</span> dev</pre></td></tr><tr><td data-num="15"></td><td><pre>&lt;<span class="token tag">!--code</span>￼42<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>编写代码</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>beans<span class="token punctuation">.</span>factory<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Value</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>context<span class="token punctuation">.</span>config<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RefreshScope</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">GetMapping</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>bind<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">RestController</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="9"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="10"></td><td><pre> * 2022/1/27</pre></td></tr><tr><td data-num="11"></td><td><pre> */</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@RestController</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token annotation punctuation">@RefreshScope</span> <span class="token comment">// 支持 Nacos 的动态刷新功能</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConfigClientController</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"$&#123;config.info&#125;"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token keyword">private</span> <span class="token class-name">String</span> configInfo<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre></pre></td></tr><tr><td data-num="18"></td><td><pre>    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/config/info"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getConfigInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="20"></td><td><pre>        <span class="token keyword">return</span> configInfo<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="21"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="23"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">43</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p><code>nacos.env</code></p>
<ul>
<li>
<div class="highlight-container" data-rel="Shell"><figure class="iseeu highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token assign-left variable">PREFER_HOST_MODE</span><span class="token operator">=</span>hostname</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token assign-left variable">NACOS_SERVERS</span><span class="token operator">=</span>nacos1:8848 nacos2:8848 nacos3:8848</pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token assign-left variable">MYSQL_SERVICE_HOST</span><span class="token operator">=</span>bj-cdb-c504ejce.sql.tencentcdb.com</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token assign-left variable">MYSQL_SERVICE_DB_NAME</span><span class="token operator">=</span>nacos_config</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token assign-left variable">MYSQL_SERVICE_PORT</span><span class="token operator">=</span><span class="token number">60161</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token assign-left variable">MYSQL_SERVICE_USER</span><span class="token operator">=</span>root</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token assign-left variable">MYSQL_SERVICE_PASSWORD</span><span class="token operator">=</span>020921lhw</pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span>--code￼44--<span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>执行 docker-compose.yml</p>
<ul>
<li>
<div class="highlight-container" data-rel="Shell"><figure class="iseeu highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">docker-compose</span> up <span class="token parameter variable">-d</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span>--code￼45--<span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h2 id="spring-cloud-alibaba-sentinel实现熔断和限流"><a class="markdownIt-Anchor" href="#spring-cloud-alibaba-sentinel实现熔断和限流">#</a> Spring Cloud Alibaba Sentinel 实现熔断和限流</h2>
<h3 id="sentinel"><a class="markdownIt-Anchor" href="#sentinel">#</a> Sentinel</h3>
<blockquote>
<p>是什么</p>
</blockquote>
<p>就是之前的 Hystrix</p>
<p>轻量级的流量控制、熔断降级 Java 库</p>
<blockquote>
<p>能干什么</p>
</blockquote>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220128170048526.png"
                      alt="image-20220128170048526"
                ></p>
<h3 id="sentinel控制台"><a class="markdownIt-Anchor" href="#sentinel控制台">#</a> Sentinel 控制台</h3>
<ul>
<li>Sentinel 由两部分构成
<ul>
<li>核心库（Java 客户端）不依赖任何框架 / 库，能够运行于所有 Java 运行时环境，同时对 Dubbo / Spring Cloud 等框架也有较好的支持</li>
<li>控制台（Dashboard）基于 Spring Boot 开发，打包后可以直接运行，不需要额外的 Tomcat 等应用容器</li>
</ul>
</li>
<li>8719 是 http 通信服务【sentinel 后台监控服务】，它监控 8401【微服务】的同时，还与 8080【sentinel 前台展示服务】交互数据，将监控到的数据在 dashboard 上展现。</li>
</ul>
<h3 id="初始化演示工程"><a class="markdownIt-Anchor" href="#初始化演示工程">#</a> 初始化演示工程</h3>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-sentinel<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 编写配置</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    server:</span><br><span class="line">      port: 8401</span><br><span class="line">    spring:</span><br><span class="line">      application:</span><br><span class="line">        name: cloud-sentinel-service-8401</span><br><span class="line">      cloud:</span><br><span class="line">        nacos:</span><br><span class="line">          discovery:</span><br><span class="line">            # Nacos服务注册中心地址</span><br><span class="line">            server-addr: 39.106.88.176:8849</span><br><span class="line">        sentinel:</span><br><span class="line">          transport:</span><br><span class="line">            # 配置 Sentinel Dashboard 地址</span><br><span class="line">            dashboard: 39.106.88.176:8858</span><br><span class="line">            # 默认8719端口，假如被占用会自动从8719开始依次+1扫描，直至找到未被占用的端口</span><br><span class="line">            port: 8719</span><br><span class="line">            clientIp: localhost</span><br><span class="line">    management:</span><br><span class="line">      endpoints:</span><br><span class="line">        web:</span><br><span class="line">          exposure:</span><br><span class="line">            include: &quot;*&quot;</span><br><span class="line">      endpoint:</span><br><span class="line">        sentinel:</span><br><span class="line">          enabled: true</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>编写启动类</p>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>client<span class="token punctuation">.</span>discovery<span class="token punctuation">.</span></span><span class="token class-name">EnableDiscoveryClient</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="8"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="9"></td><td><pre> * 2022/1/28</pre></td></tr><tr><td data-num="10"></td><td><pre> */</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token annotation punctuation">@EnableDiscoveryClient</span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainApp8401</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">MainApp8401</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">47</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="流控规则"><a class="markdownIt-Anchor" href="#流控规则">#</a> 流控规则</h3>
<h4 id="基本介绍"><a class="markdownIt-Anchor" href="#基本介绍">#</a> 基本介绍</h4>
<ul>
<li>资源名
<ul>
<li>唯一名称，默认请求路径</li>
</ul>
</li>
<li>针对来源
<ul>
<li>Sentinel 可以针对调用者进行限流，填写微服务名，默认 default（不区分来源）</li>
</ul>
</li>
<li>阈值类型 / 单机阈值
<ul>
<li>QPS（每秒钟的请求数量）
<ul>
<li>当调用该 api 的 QPS 达到阈值的时候，进行限流</li>
</ul>
</li>
<li>线程数
<ul>
<li>当调用该 api 的线程数达到阈值的时候，进行限流</li>
</ul>
</li>
</ul>
</li>
<li>是否集群
<ul>
<li>不需要集群</li>
</ul>
</li>
<li>流控模式
<ul>
<li>直接：
<ul>
<li>api 达到限流条件时，直接限流</li>
</ul>
</li>
<li>关联
<ul>
<li>当关联的资源达到阈值时，就限流自己</li>
</ul>
</li>
<li>链路
<ul>
<li>只记录指定链路上的流量（指定资源从入口资源进来的流量，如果达到阈值，就进行限流）【api 级别的针对来源】</li>
</ul>
</li>
</ul>
</li>
<li>流控效果
<ul>
<li>快速失败
<ul>
<li>直接失败，抛异常</li>
</ul>
</li>
<li>Warm Up
<ul>
<li>根据 codeFactor（冷加载因子，默认 3）的值，从阈值 /codeFactor，经过预热时长，才达到设置的 QPS 阈值</li>
</ul>
</li>
<li>排队等待
<ul>
<li>匀速排队，让请求以匀速的速度通过，阈值类型必须设置为 QPS，否则无效</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>​</p>
<h4 id="流控模式"><a class="markdownIt-Anchor" href="#流控模式">#</a> 流控模式</h4>
<blockquote>
<p>直接 (默认)</p>
</blockquote>
<ul>
<li>
<p>直接 -&gt; 快速失败</p>
<ul>
<li>系统默认</li>
</ul>
</li>
<li>
<p>配置及说明</p>
<ul>
<li>表示一秒钟内查询 1 次就是 OK，若超过次数 1，就直接 - 快速失败，报默认错误</li>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220130111911145.png"
                      alt="image-20220130111911145"
                ></li>
</ul>
</li>
<li>
<p>添加自定义的兜底方法</p>
</li>
</ul>
<p>默认 - 自定义</p>
<blockquote>
<p>关联</p>
</blockquote>
<ul>
<li>是什么
<ul>
<li>当关联的资源达到阈值时就限流自己</li>
<li>当与 A 关联的资源 B 达到阈值后，就限流自己</li>
<li>B 惹事，A 挂了</li>
</ul>
</li>
<li>配置 A
<ul>
<li>设置效果
<ul>
<li>当关联资源 /testB 的 qps 阈值超过 1 时，就限流 /testA 的 Rest 访问地址，<mark>当关联资源到阈值后限制配置好的资源名</mark></li>
</ul>
</li>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220130114025348.png"
                      alt="image-20220130114025348"
                ></li>
<li>当大批量访问 B，访问 A 发现被限流了</li>
</ul>
</li>
</ul>
<blockquote>
<p>链路</p>
</blockquote>
<ul>
<li>
<p>只针对从指定链路访问到本资源的请求做统计，判断是否超过阈值</p>
</li>
<li>
<p>案例</p>
<ul>
<li>
<p>有查询订单和创建订单业务，两者都需要查询商品。针对从查询订单进入到查询商品的请求统计，并设置限流。</p>
</li>
<li>
<p>步骤：<br>
1. 在 OrderService 中添加一个 queryGoods 方法，不用实现业务<br>
 2. 在 OrderController 中，改造 /order/query 端点，调用 OrderService 中的 queryGoods 方法 (/order/query -&gt; queryGoods)<br>
 3. 在 OrderController 中添加一个 /order/save 的端点，调用 OrderService 的 queryGoods 方法 (/order/save -&gt; queryGoods)<br>
 4. 给 queryGoods 设置限流规则，从 /order/query 进入 queryGoods 的方法限制 QPS 必须小于 2**（设置 /order/query qqs&lt;2）**</p>
<p>Sentinel 默认只标记 Controller 中的方法为资源，如果要标记其它方法，需要利用 @SentinelResource 注解，示例：</p>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SentinelResource(&quot;goods&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">queryGoods</span><span class="params">()</span>&#123;</span><br><span class="line">    System.err.println(<span class="string">&quot;查询商品&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
</li>
<li>
<p>Sentinel 默认会将 Controller 方法做 context 整合，导致链路模式的流控失效，需要修改 application.yml，添加配置</p>
</li>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre>spring<span class="token operator">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  cloud<span class="token operator">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    sentinel<span class="token operator">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>      transport<span class="token operator">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>        dashboard<span class="token operator">:</span> localhost<span class="token operator">:</span><span class="token number">8080</span> # sentinel控制台地址</pre></td></tr><tr><td data-num="6"></td><td><pre>      web<span class="token operator">-</span>context<span class="token operator">-</span>unify<span class="token operator">:</span> <span class="token boolean">false</span> # 关闭context整合</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">49</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>编写配置</p>
<ul>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220205170840600.png"
                      alt="image-20220205170840600"
                ></li>
</ul>
</li>
</ul>
</li>
<li>
<p>参数例外项</p>
<ul>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220205171351345.png"
                      alt="image-20220205171351345"
                ></li>
<li>注意
<ul>
<li>参数类型必须是基本类型</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><mark> <code>@SentinelResource</code>  主管配置出错，运行出错该走异常走异常</mark></p>
<h3 id="系统规则"><a class="markdownIt-Anchor" href="#系统规则">#</a> 系统规则</h3>
<blockquote>
<p>是什么</p>
</blockquote>
<p>系统保护规则是从应用级别的入口流量进行控制，从单台机器的 load、CPU 使用率、平均 RT、入口 QPS 和并发线程数等几个维度监控应用指标，让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。</p>
<p>系统保护规则是应用整体维度的，而不是资源维度的，并且<strong>仅对入口流量生效</strong>。入口流量指的是进入应用的流量（ <code>EntryType.IN</code> ），比如 Web 服务或 Dubbo 服务端接收的请求，都属于入口流量。</p>
<p>系统规则支持以下的模式：</p>
<ul>
<li><strong>Load 自适应</strong>（仅对 Linux/Unix-like 机器生效）：系统的 load1 作为启发指标，进行自适应系统保护。当系统 load1 超过设定的启发值，且系统当前的并发线程数超过估算的系统容量时才会触发系统保护（BBR 阶段）。系统容量由系统的  <code>maxQps * minRt</code>  估算得出。设定参考值一般是  <code>CPU cores * 2.5</code> 。</li>
<li><strong>CPU usage</strong>（1.5.0+ 版本）：当系统 CPU 使用率超过阈值即触发系统保护（取值范围 0.0-1.0），比较灵敏。</li>
<li><strong>平均 RT</strong>：当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护，单位是毫秒。</li>
<li><strong>并发线程数</strong>：当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。</li>
<li><strong>入口 QPS</strong>：当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。</li>
</ul>
<h3 id="sentinelresource"><a class="markdownIt-Anchor" href="#sentinelresource">#</a> @SentinelResource</h3>
<p>自定义限流处理逻辑</p>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping(&quot;/testByResource&quot;)</span></span><br><span class="line"><span class="meta">@SentinelResource(value = &quot;testByResource&quot;, blockHandler = &quot;testByResourceHandler&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">testByResource</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;按资源名称限流&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">testByResourceHandler</span><span class="params">(BlockException exception)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> exception.getClass().getCanonicalName() + <span class="string">&quot;服务不可用&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<blockquote>
<p>注意</p>
</blockquote>
<ul>
<li>按路径时，自定义的限流逻辑不起作用</li>
</ul>
<blockquote>
<p>自定义限流处理类</p>
</blockquote>
<ul>
<li>
<div class="highlight-container" data-rel="Java"><figure class="iseeu highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wenhe9<span class="token punctuation">.</span>springcloud<span class="token punctuation">.</span>handler</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>alibaba<span class="token punctuation">.</span>csp<span class="token punctuation">.</span>sentinel<span class="token punctuation">.</span>slots<span class="token punctuation">.</span>block<span class="token punctuation">.</span></span><span class="token class-name">BlockException</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment">/**</pre></td></tr><tr><td data-num="6"></td><td><pre> * @author DuJinliang</pre></td></tr><tr><td data-num="7"></td><td><pre> * 2022/2/5</pre></td></tr><tr><td data-num="8"></td><td><pre> */</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CustomBlockHandler</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">customBlockHandler1</span><span class="token punctuation">(</span><span class="token class-name">BlockException</span> exception<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"自定义的限流 --- 1"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">String</span> <span class="token function">customBlockHandler2</span><span class="token punctuation">(</span><span class="token class-name">BlockException</span> exception<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token keyword">return</span> <span class="token string">"自定义的限流 --- 2"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">51</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<p>注意，这里一定要是要静态方法</p>
</li>
</ul>
<p><code>@SentinelResource</code>  用于定义资源，并提供可选的异常处理和 fallback 配置项。  <code>@SentinelResource</code>  注解包含以下属性：</p>
<ul>
<li><code>value</code> ：资源名称，必需项（不能为空）</li>
<li><code>entryType</code> ：entry 类型，可选项（默认为  <code>EntryType.OUT</code> ）</li>
<li><code>blockHandler</code>  /  <code>blockHandlerClass</code> :  <code>blockHandler</code>  对应处理  <code>BlockException</code>  的函数名称，可选项。blockHandler 函数访问范围需要是  <code>public</code> ，返回类型需要与原方法相匹配，参数类型需要和原方法相匹配并且最后加一个额外的参数，类型为  <code>BlockException</code> 。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数，则可以指定  <code>blockHandlerClass</code>  为对应的类的  <code>Class</code>  对象，注意对应的函数必需为 static 函数，否则无法解析。</li>
<li><code>fallback</code> / <code>fallbackClass</code> ：fallback 函数名称，可选项，用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常（除了 <code>exceptionsToIgnore</code>  里面排除掉的异常类型）进行处理。fallback 函数签名和位置要求：
<ul>
<li>返回值类型必须与原函数返回值类型一致；</li>
<li>方法参数列表需要和原函数一致，或者可以额外多一个  <code>Throwable</code>  类型的参数用于接收对应的异常。</li>
<li>fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数，则可以指定  <code>fallbackClass</code>  为对应的类的  <code>Class</code>  对象，注意对应的函数必需为 static 函数，否则无法解析。</li>
</ul>
</li>
<li><code>defaultFallback</code> （since 1.6.0）：默认的 fallback 函数名称，可选项，通常用于通用的 fallback 逻辑（即可以用于很多服务或方法）。默认 fallback 函数可以针对所有类型的异常（除了 <code>exceptionsToIgnore</code>  里面排除掉的异常类型）进行处理。若同时配置了 fallback 和 defaultFallback，则只有 fallback 会生效。defaultFallback 函数签名要求：
<ul>
<li>返回值类型必须与原函数返回值类型一致；</li>
<li>方法参数列表需要为空，或者可以额外多一个  <code>Throwable</code>  类型的参数用于接收对应的异常。</li>
<li>defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数，则可以指定  <code>fallbackClass</code>  为对应的类的  <code>Class</code>  对象，注意对应的函数必需为 static 函数，否则无法解析。</li>
</ul>
</li>
<li><code>exceptionsToIgnore</code> （since 1.6.0）：用于指定哪些异常被排除掉，不会计入异常统计中，也不会进入 fallback 逻辑中，而是会原样抛出。</li>
</ul>
<p>1.8.0 版本开始， <code>defaultFallback</code>  支持在类级别进行配置。</p>
<blockquote>
<p>注：1.6.0 之前的版本 fallback 函数只针对降级异常（ <code>DegradeException</code> ）进行处理，<strong>不能针对业务异常进行处理</strong>。</p>
</blockquote>
<p>特别地，若 blockHandler 和 fallback 都进行了配置，则被限流降级而抛出  <code>BlockException</code>  时只会进入  <code>blockHandler</code>  处理逻辑。若未配置  <code>blockHandler</code> 、 <code>fallback</code>  和  <code>defaultFallback</code> ，则被限流降级时会将  <code>BlockException</code>  <strong>直接抛出</strong>（若方法本身未定义 throws BlockException 则会被 JVM 包装一层  <code>UndeclaredThrowableException</code> ）。</p>
<h3 id="持久化"><a class="markdownIt-Anchor" href="#持久化">#</a> 持久化</h3>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.csp<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>sentinel-datasource-nacos<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 编写配置</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    server:</span><br><span class="line">      port: 8401</span><br><span class="line">    spring:</span><br><span class="line">      application:</span><br><span class="line">        name: cloud-sentinel-service-8401</span><br><span class="line">      cloud:</span><br><span class="line">        nacos:</span><br><span class="line">          discovery:</span><br><span class="line">            # Nacos服务注册中心地址</span><br><span class="line">            server-addr: 39.106.88.176:8849</span><br><span class="line">        sentinel:</span><br><span class="line">          transport:</span><br><span class="line">            # 配置 Sentinel Dashboard 地址</span><br><span class="line">            dashboard: http://localhost:8858</span><br><span class="line">          eager: true</span><br><span class="line">          web-context-unify: false</span><br><span class="line">          datasource:</span><br><span class="line">            ds1:</span><br><span class="line">              nacos:</span><br><span class="line">                serverAddr: 39.106.88.176:8849</span><br><span class="line">                dataId: $&#123;spring.application.name&#125;</span><br><span class="line">                groupId: DEFAULT_GROUP</span><br><span class="line">                dataType: json</span><br><span class="line">                ruleType: flow</span><br><span class="line">                username: nacos</span><br><span class="line">                password: nacos</span><br><span class="line">    management:</span><br><span class="line">      endpoints:</span><br><span class="line">        web:</span><br><span class="line">          exposure:</span><br><span class="line">            include: &quot;*&quot;</span><br><span class="line">      endpoint:</span><br><span class="line">        sentinel:</span><br><span class="line">          enabled: true</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p>在 nacos 配置中心进行配置</p>
<ul>
<li>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220205215859164.png"
                      alt="image-20220205215859164"
                ></p>
</li>
<li>
<p>其中</p>
<ul>
<li>resource 表示资源名称</li>
<li>limitApp 表示针对来源</li>
<li>grade 表示阈值类型</li>
<li>count 表示阈值</li>
<li>strategy 表示流控模式</li>
<li>controlBehavior 表示流控效果</li>
<li>clusterMode 表示是否集群</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="spring-cloud-alibaba-seata-处理分布式事务"><a class="markdownIt-Anchor" href="#spring-cloud-alibaba-seata-处理分布式事务">#</a> Spring Cloud Alibaba Seata 处理分布式事务</h2>
<h3 id="分布式事务问题"><a class="markdownIt-Anchor" href="#分布式事务问题">#</a> 分布式事务问题</h3>
<ul>
<li>分布式之前
<ul>
<li>单机单库没有问题</li>
</ul>
</li>
<li>分布式之后
<ul>
<li>单体应用被拆分成微服务应用，原来的三个模块被拆分成三个独立的应用，分别使用三个独立的数据源。业务操作需要调用三个服务来完成，此时<mark>每隔服务内部的数据一致性由本地事务来保证，但是全局的数据一致性问题没法保证</mark></li>
<li><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220206155229696.png"
                      alt="image-20220206155229696"
                ></li>
</ul>
</li>
</ul>
<h3 id="seata简介"><a class="markdownIt-Anchor" href="#seata简介">#</a> Seata 简介</h3>
<blockquote>
<p>是什么</p>
</blockquote>
<ul>
<li>Seata 是一款开源的分布式事务解决方案，致力于在微服务架构下提供高性能和简单易用的分布式事务服务</li>
</ul>
<blockquote>
<p>能干什么</p>
</blockquote>
<ul>
<li>
<p>一个典型的分布式事务过程</p>
<ul>
<li>分布式事务处理过程的一 ID 和三组件模型
<ul>
<li>Transaction ID XID
<ul>
<li>全局唯一的事务 ID</li>
</ul>
</li>
<li>三组件概念
<ul>
<li>Transaction Coordinator(TC)
<ul>
<li>事务协调者</li>
<li>维护全局和分支事务的状态，驱动全局事务的提交和回滚</li>
</ul>
</li>
<li>Transaction Manager™
<ul>
<li>事务管理器</li>
<li>定义全局事务的范围：开始全局事务、提交和回滚全局事务</li>
</ul>
</li>
<li>Resource Manager(RM)
<ul>
<li>资源管理器</li>
<li>管理分支事务处理的资源，与 TC 交谈以注册分支事务和报告分支事务的状态，并驱动分支事务的提交和回滚</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>处理过程</p>
<ol>
<li>TM 向 TC 申请开启一个全局事务，全局事务创建成功并生成一个全局唯一的 XID</li>
<li>XID 在微服务调用链路的上下文中传播</li>
<li>RM 向 TC 注册分支事务，将其纳入 XID 对应全局事务的管辖</li>
<li>TM 向 TC 发起针对 XID 的全局提交或回滚决议</li>
<li>TC 调度 XID 下管辖的全部分支事务完成提交或回滚请求</li>
</ol>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220206173145685.png"
                      alt="image-20220206173145685"
                ></p>
</li>
</ul>
<h3 id="启动与配置"><a class="markdownIt-Anchor" href="#启动与配置">#</a> 启动与配置</h3>
<p><a class="link"   target="_blank" rel="noopener" href="https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose.html" >https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose.html <i class="fa-regular fa-arrow-up-right-from-square fa-sm"></i></a></p>
<ol>
<li>
<p>Docker-Compose 部署 nacos 注册中心 db 存储</p>
<ul>
<li>
<p>docker-compose.yaml</p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">'3.1'</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token key atrule">services</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>  <span class="token key atrule">seata-server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token key atrule">image</span><span class="token punctuation">:</span> seataio/seata<span class="token punctuation">-</span>server<span class="token punctuation">:</span><span class="token number">1.4</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">hostname</span><span class="token punctuation">:</span> seata<span class="token punctuation">-</span>server</pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token key atrule">ports</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>      <span class="token punctuation">-</span> <span class="token string">"8091:8091"</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token key atrule">environment</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre>      <span class="token comment"># 指定 seata 服务启动端口</span></pre></td></tr><tr><td data-num="10"></td><td><pre>      <span class="token punctuation">-</span> SEATA_PORT=8091</pre></td></tr><tr><td data-num="11"></td><td><pre>      <span class="token comment"># 注册到 nacos 上的 ip，客户端将通过该 ip 访问 seata 服务</span></pre></td></tr><tr><td data-num="12"></td><td><pre>      <span class="token comment"># 注意公网 ip 和内网 ip 的差异</span></pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token punctuation">-</span> SEATA_IP=39.106.88.176</pre></td></tr><tr><td data-num="14"></td><td><pre>      <span class="token punctuation">-</span> SEATA_CONFIG_NAME=file<span class="token punctuation">:</span>/root/seata<span class="token punctuation">-</span>config/registry.conf</pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token key atrule">volumes</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="16"></td><td><pre>      <span class="token comment"># 因为 registry.conf 中是 nacos 配置中心，只需要把 registry.conf 放到 /root/seata-server/config 文件夹中</span></pre></td></tr><tr><td data-num="17"></td><td><pre>      <span class="token punctuation">-</span> <span class="token string">"/root/seata-server/config:/root/seata-config"</span></pre></td></tr><tr><td data-num="18"></td><td><pre>&lt;<span class="token tag">!--code</span>￼53<span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
<li>
<p>在容器编排之前，完善数据库</p>
<ul>
<li>
<pre><code class="language-mysql">-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME(6),
    `gmt_modified`      DATETIME(6),
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(128),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `status`         TINYINT      NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_status` (`status`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
    `lock_key`       CHAR(20) NOT NULL,
    `lock_value`     VARCHAR(20) NOT NULL,
    `expire`         BIGINT,
    primary key (`lock_key`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 在nacos编写配置</span><br><span class="line"></span><br><span class="line">  - dataId</span><br><span class="line"></span><br><span class="line">    - `` seataServer.properties``</span><br><span class="line"></span><br><span class="line">  - Group</span><br><span class="line"></span><br><span class="line">    - ``SEATA_GROUP``</span><br><span class="line"></span><br><span class="line">  - ```properties</span><br><span class="line">    # 存储模式</span><br><span class="line">    store.mode=db</span><br><span class="line">    </span><br><span class="line">    store.db.datasource=druid</span><br><span class="line">    store.db.dbType=mysql</span><br><span class="line">    # 需要根据mysql的版本调整driverClassName</span><br><span class="line">    # mysql8及以上版本对应的driver：com.mysql.cj.jdbc.Driver</span><br><span class="line">    # mysql8以下版本的driver：com.mysql.jdbc.Driver</span><br><span class="line">    store.db.driverClassName=com.mysql.cj.jdbc.Driver</span><br><span class="line">    # 注意根据生产实际情况调整参数host和port</span><br><span class="line">    store.db.url=jdbc:mysql://bj-cdb-c504ejce.sql.tencentcdb.com:60161/seata_server?useUnicode=true&amp;characterEncoding=utf8&amp;connectTimeout=1000&amp;socketTimeout=3000&amp;autoReconnect=true&amp;useSSL=false</span><br><span class="line">    # 数据库用户名</span><br><span class="line">    store.db.user=root</span><br><span class="line">    # 用户名密码</span><br><span class="line">    store.db.password=020921lhw</span><br></pre></td></tr></table></figure></div>

</code></pre>
</li>
</ul>
</li>
<li>
<p>dataId</p>
<ul>
<li><code>service.vgroupMapping.default_tx_group</code></li>
</ul>
</li>
<li>
<p>Group</p>
<ul>
<li><code>SEATA_GROUP</code></li>
</ul>
</li>
<li>
<p>内容</p>
<ul>
<li>
<div class="highlight-container" data-rel="Text"><figure class="iseeu highlight text"><figcaption data-lang="text"></figcaption><table><tr><td data-num="1"></td><td><pre>seata-server</pre></td></tr><tr><td data-num="2"></td><td><pre><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- docker-compose运行</span><br><span class="line"></span><br><span class="line">  - ```dockerfile</span><br><span class="line">    docker-compose up -d</span><br></pre></td></tr></table></figure></div></div></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
</li>
<li>
<p>编写代码</p>
<ul>
<li>
<p>导入依赖</p>
<ul>
<li>
<div class="highlight-container" data-rel="Xml"><figure class="iseeu highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token prolog">&lt;?xml version="1.0" encoding="UTF-8"?></span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">"</span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>cn.wenhe9.springcloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>cn.wenhe9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">></span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="11"></td><td><pre></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>seata-order-service-2001<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.source</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.source</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="16"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>maven.compiler.target</span><span class="token punctuation">></span></span>8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>maven.compiler.target</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="18"></td><td><pre></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="20"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="21"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="22"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="23"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="24"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="25"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="26"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-config<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="27"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="28"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="29"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="30"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-alibaba-seata<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="31"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusions</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="32"></td><td><pre>                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>exclusion</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="33"></td><td><pre>                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>io.seata<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="34"></td><td><pre>                    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>seata-all<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="35"></td><td><pre>                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusion</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="36"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>exclusions</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="37"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="38"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="39"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>io.seata<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="40"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>seata-all<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="41"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.4.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="42"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="43"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="44"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="45"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-openfeign<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="46"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="47"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="48"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="49"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="50"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="51"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="52"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="53"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-boot-starter-actuator<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="54"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="55"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="56"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="57"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="58"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="59"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="60"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="61"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>druid-spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="62"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="63"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="64"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.mybatis.spring.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="65"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>mybatis-spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="66"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="67"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="68"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>cn.wenhe9<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="69"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>cloud-api-commons<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="70"></td><td><pre>            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">></span></span>1.0-SNAPSHOT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="71"></td><td><pre>        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="72"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="73"></td><td><pre></pre></td></tr><tr><td data-num="74"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="75"></td><td><pre><span class="token comment"><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 编写配置</span><br><span class="line"></span><br><span class="line">- ``bootstrap.yml``</span><br><span class="line"></span><br><span class="line">  - ```yaml</span><br><span class="line">    # 抽取出nacos的公共配置 方便修改 启动配置中只配置nacos的 其他的在application.yml</span><br><span class="line">    nacos:</span><br><span class="line">      service-address: 39.106.88.176:8849  #nacos地址需要带端口80也需要</span><br><span class="line">      namespace: 19d81780-9128-4e5d-89f5-a26591a9ee99 # nacos命名空间 比如dev 开发环境</span><br><span class="line">      group: SEATA_GROUP # nacos分组 比如 XXX业务组</span><br><span class="line">      username: nacos</span><br><span class="line">      password: nacos</span><br><span class="line">      seata:</span><br><span class="line">        server: seata-server # 这里必须和seata注册到nacos的服务名一样</span><br><span class="line">        group: SEATA_GROUP # seata服务注册到nacos的组名seata-group</span><br><span class="line">    </span><br><span class="line">    # 配置注册中心</span><br><span class="line">    spring:</span><br><span class="line">      application:</span><br><span class="line">        name: seata-order-service # 应用名称</span><br><span class="line">      main:</span><br><span class="line">        allow-bean-definition-overriding: true # 解决bean重复定义问题</span><br><span class="line">      cloud:</span><br><span class="line">        nacos:</span><br><span class="line">          discovery:</span><br><span class="line">            server-addr: $&#123;nacos.service-address&#125; # nacos服务注册地址</span><br><span class="line">            namespace: $&#123;nacos.namespace&#125; # nacos注册的命名空间</span><br><span class="line">            group: $&#123;nacos.group&#125; # nacos注册的group</span><br><span class="line">          config:</span><br><span class="line">            server-addr: $&#123;nacos.service-address&#125; # nacos配置中心地址</span><br><span class="line">            namespace: $&#123;nacos.namespace&#125; # nacos配置中心的命名空间</span><br><span class="line">            group: $&#123;nacos.group&#125; # nacos配置中心的group</span><br><span class="line">            file-extension: yml # 程序读取的主配置文件 拼接起来为 seata-order-service.yml (spring.application.name + spring.profiles.active + file-extension)</span><br><span class="line">            extension-configs[0]: # 第一个扩展配置(seata-order-service.yml有配置相同项将覆盖下面配置)</span><br><span class="line">              data-id: common.yml # 配置文件名字</span><br><span class="line">              group: $&#123;nacos.group&#125; # 配置文件所属分组</span><br><span class="line">              refresh: true # 是否动态刷新</span><br></pre></td></tr></table></figure></div></div></span></pre></td></tr></table></figure></li>
</ul>
</li>
<li>
<p><code>application.yml</code></p>
<ul>
<li>
<div class="highlight-container" data-rel="Yaml"><figure class="iseeu highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">2001</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">datasource</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">type</span><span class="token punctuation">:</span> com.alibaba.druid.pool.DruidDataSource</pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token key atrule">driver-class-name</span><span class="token punctuation">:</span> com.mysql.cj.jdbc.Driver</pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>//bj<span class="token punctuation">-</span>cdb<span class="token punctuation">-</span>c504ejce.sql.tencentcdb.com<span class="token punctuation">:</span>60161/seata_order<span class="token punctuation">?</span>useUnicode=true<span class="token important">&amp;characterEncoding=utf-8&amp;useSSL=false</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token key atrule">username</span><span class="token punctuation">:</span> root</pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token key atrule">password</span><span class="token punctuation">:</span> <span class="token string">"020921lhw"</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token comment"># seata 配置</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token key atrule">seata</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="12"></td><td><pre>  <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> <span class="token comment"># 开启 seata 支持</span></pre></td></tr><tr><td data-num="13"></td><td><pre>  <span class="token key atrule">tx-service-group</span><span class="token punctuation">:</span> default_tx_group <span class="token comment"># 这里需要和 config.txt 文件中的 service.vgroupMpping:my_test_tx_group=default 一致</span></pre></td></tr><tr><td data-num="14"></td><td><pre>  <span class="token key atrule">service</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    <span class="token key atrule">vgroup-mapping</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="16"></td><td><pre>      <span class="token key atrule">default_tx_group</span><span class="token punctuation">:</span> seata<span class="token punctuation">-</span>server</pre></td></tr><tr><td data-num="17"></td><td><pre>  <span class="token key atrule">enable-auto-data-source-proxy</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="18"></td><td><pre>  <span class="token comment"># seata 整合 nacos 配置中心</span></pre></td></tr><tr><td data-num="19"></td><td><pre>  <span class="token key atrule">config</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="20"></td><td><pre>    <span class="token key atrule">type</span><span class="token punctuation">:</span> nacos</pre></td></tr><tr><td data-num="21"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="22"></td><td><pre>      <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.service<span class="token punctuation">-</span>address<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="23"></td><td><pre>      <span class="token key atrule">group</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.seata.group<span class="token punctuation">&#125;</span> <span class="token comment"># seata 服务注册到 nacos 的组名 SEATA_GROUP</span></pre></td></tr><tr><td data-num="24"></td><td><pre>      <span class="token key atrule">namespace</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.namespace<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="25"></td><td><pre>      <span class="token key atrule">username</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.username<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="26"></td><td><pre>      <span class="token key atrule">password</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.password<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="27"></td><td><pre>  <span class="token key atrule">registry</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="28"></td><td><pre>    <span class="token key atrule">type</span><span class="token punctuation">:</span> nacos</pre></td></tr><tr><td data-num="29"></td><td><pre>    <span class="token key atrule">nacos</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="30"></td><td><pre>      <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.service<span class="token punctuation">-</span>address<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="31"></td><td><pre>      <span class="token key atrule">group</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.seata.group<span class="token punctuation">&#125;</span> <span class="token comment"># seata 服务注册到 nacos 的组名 SEATA_GROUP</span></pre></td></tr><tr><td data-num="32"></td><td><pre>      <span class="token key atrule">application</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.seata.server<span class="token punctuation">&#125;</span> <span class="token comment"># 这里必须和 seata 注册到 nacos 的服务名一样默认 seata-server</span></pre></td></tr><tr><td data-num="33"></td><td><pre>      <span class="token key atrule">namespace</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.namespace<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="34"></td><td><pre>      <span class="token key atrule">username</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.username<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="35"></td><td><pre>      <span class="token key atrule">password</span><span class="token punctuation">:</span> $<span class="token punctuation">&#123;</span>nacos.password<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="36"></td><td><pre><span class="token key atrule">mybatis</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="37"></td><td><pre>  <span class="token key atrule">mapper-locations</span><span class="token punctuation">:</span> classpath<span class="token punctuation">:</span>mapper/<span class="token important">*.xml</span></pre></td></tr><tr><td data-num="38"></td><td><pre>  <span class="token key atrule">type-aliases-package</span><span class="token punctuation">:</span> cn.wenhe9.springcloud.entities</pre></td></tr><tr><td data-num="39"></td><td><pre><span class="token key atrule">feign</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="40"></td><td><pre>  <span class="token key atrule">hystrix</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="41"></td><td><pre>    <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">false</span></pre></td></tr><tr><td data-num="42"></td><td><pre><span class="token key atrule">management</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="43"></td><td><pre>  <span class="token key atrule">endpoints</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="44"></td><td><pre>    <span class="token key atrule">web</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="45"></td><td><pre>      <span class="token key atrule">exposure</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="46"></td><td><pre>        <span class="token key atrule">include</span><span class="token punctuation">:</span> <span class="token string">"*"</span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
</ol>
<p>SEATA 的执行流程</p>
<ol>
<li>TM 开启分布式事务（TM 向 TC 注册全局事务记录）</li>
<li>按业务场景，编排数据库、等事务内资源（RM 向 TC 汇报资源准备状态）</li>
<li>TM 结束分布式事务，事务一阶段结束（TM 通知 TC 提交 / 回滚分布式事务）</li>
<li>TC 汇总事务信息，决定分布式事务是提交还是回滚</li>
<li>TC 通知所有 RM 提交 / 回滚资源，事务二阶段结束</li>
</ol>
<h3 id="at原理"><a class="markdownIt-Anchor" href="#at原理">#</a> AT 原理</h3>
<ul>
<li>SEATA 如何实现无侵入式的事务管理</li>
</ul>
<blockquote>
<p>一阶段加载</p>
</blockquote>
<ul>
<li>在一阶段，SEATA 会拦截 “业务 SQL”
<ol>
<li>解析 SQL 语义，找到业务 SQL 要更新的业务数据，在业务数据被更新之前，将其保存成 <code>before image</code></li>
<li>执行业务 SQL 更新业务数据，在业务数据更新之后</li>
<li>将其保存成 <code>after image</code> ，最后生成行锁</li>
</ol>
</li>
<li>以上操作全部在一个数据库事务内完成，这样保证了一阶段操作的原子性</li>
</ul>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220208152334509.png"
                      alt="image-20220208152334509"
                ></p>
<blockquote>
<p>二阶段提交</p>
</blockquote>
<ul>
<li>
<p>二阶段如是顺利提交的话，因为业务 SQL 在一阶段已经提交给数据库，所以 SEATA 框架只需将<mark>一阶段保存的快照数据和行锁删掉，完成数据请立即可</mark></p>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220208152556235.png"
                      alt="image-20220208152556235"
                ></p>
</li>
</ul>
<blockquote>
<p>二阶段回滚</p>
</blockquote>
<ul>
<li>二阶段如果是回滚的话，SEATA 就需要回滚一阶段已经执行的业务 SQL，还原业务数据</li>
<li>回滚方式便是利用 <code>before image</code>  还原业务数据，但在还原前首先要校验脏写，对比数据库当前业务数据和 <code>after image</code> ，如果两份数据完全一致就说明没有脏写，可以还原业务数据，如果不一致就说明有脏写，出现脏写就需要人工处理</li>
</ul>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220208152953670.png"
                      alt="image-20220208152953670"
                ></p>
<p><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="http://tuchuang.wenhe9.cn/image-20220208153436295.png"
                      alt="image-20220208153436295"
                ></p>

            </div>

            
                <div class="post-copyright-info">
                    <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> Spring Cloud 技术栈</li>
        <li><strong>作者:</strong> meteor</li>
        <li><strong>创建于:</strong> 2023-06-05 17:05:40</li>
        
            <li>
                <strong>更新于:</strong> 2023-06-05 01:06:07
            </li>
        
        <li>
            <strong>链接:</strong> https://gitee.com/du-jinliang/2023/06/06/Spring-Cloud-技术栈/
        </li>
        <li>
            <strong>版权声明:</strong> 本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
        </li>
    </ul>
</div>

                </div>
            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/Spring-Cloud/">#Spring Cloud</a>&nbsp;
                        </li>
                    
                </ul>
            

            

            
                <div class="article-nav">
                    
                        <div class="article-prev">
                            <a class="prev"
                            rel="prev"
                            href="/2023/06/06/SpringSecurity/"
                            >
                                <span class="left arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-left"></i>
                                </span>
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">SpringSecurity</span>
                                    <span class="post-nav-item">上一篇</span>
                                </span>
                            </a>
                        </div>
                    
                    
                        <div class="article-next">
                            <a class="next"
                            rel="next"
                            href="/2023/06/06/Service-Mesh/"
                            >
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">Service Mesh</span>
                                    <span class="post-nav-item">下一篇</span>
                                </span>
                                <span class="right arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-right"></i>
                                </span>
                            </a>
                        </div>
                    
                </div>
            


            
                <div class="comment-container">
                    <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fa-solid fa-comments"></i>&nbsp;评论
    </div>
    

        
            
    <div id="gitalk-container"></div>
    <script data-pjax
            src="//cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js"></script>
    <script data-pjax>

        function loadGitalk() {
            let __gitalk__pathname = decodeURI(location.pathname);
            const __gitalk__pathnameLength = __gitalk__pathname.length;
            const __gitalk__pathnameMaxLength = 50;
            if (__gitalk__pathnameLength > __gitalk__pathnameMaxLength) {
                __gitalk__pathname = __gitalk__pathname.substring(0, __gitalk__pathnameMaxLength - 3) + '...';
            }

            try {
                Gitalk && new Gitalk({
                    clientID: '55bad54a77b7e60ad62d',
                    clientSecret: '1031c81500c6be06e338087cb7b713f2d0201b46',
                    repo: 'wait-you.github.io',
                    owner: 'wait-you',
                    admin: ['wait-you'],
                    id: 'comment',
                    language: 'zh-CN'
                }).render('gitalk-container');

            } catch (e) {
                window.Gitalk = null;
            }
        }

        if ('true') {
            const loadGitalkTimeout = setTimeout(() => {
                loadGitalk();
                clearTimeout(loadGitalkTimeout);
            }, 1000);
        } else {
            window.addEventListener('DOMContentLoaded', loadGitalk);
        }
    </script>



        
    
</div>

                </div>
            
        </div>

        
            <div class="toc-content-container">
                <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">Spring Cloud 技术栈</div>
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#spring-cloud-%E6%8A%80%E6%9C%AF%E6%A0%88"><span class="nav-text"> Spring Cloud 技术栈</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%89%88%E6%9C%AC%E9%80%89%E6%8B%A9"><span class="nav-text"> 版本选择</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#springboot%E7%89%88%E6%9C%AC%E9%80%89%E6%8B%A9"><span class="nav-text"> SpringBoot 版本选择</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#springcloud%E7%89%88%E6%9C%AC%E9%80%89%E6%8B%A9"><span class="nav-text"> SpringCloud 版本选择</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#springboot-%E5%92%8C-springcloud%E7%89%88%E6%9C%AC%E5%AF%B9%E5%BA%94%E6%9F%A5%E7%9C%8B"><span class="nav-text"> SpringBoot 和 SpringCloud 版本对应查看</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B3%E4%BA%8Ecloud%E5%90%84%E7%A7%8D%E7%BB%84%E4%BB%B6%E7%9A%84%E5%81%9C%E6%9B%B4%E5%8D%87%E7%BA%A7%E6%9B%BF%E6%8D%A2"><span class="nav-text"> 关于 Cloud 各种组件的停更 &#x2F; 升级 &#x2F; 替换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E7%BC%96%E7%A0%81%E6%9E%84%E5%BB%BA"><span class="nav-text"> 微服务架构编码构建</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#idea%E6%96%B0%E5%BB%BAproject%E5%B7%A5%E4%BD%9C%E7%A9%BA%E9%97%B4"><span class="nav-text"> IDEA 新建 Project 工作空间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#actuator%E5%BE%AE%E6%9C%8D%E5%8A%A1%E4%BF%A1%E6%81%AF%E5%AE%8C%E6%95%B4"><span class="nav-text"> actuator 微服务信息完整</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E5%8F%91%E7%8E%B0discovery"><span class="nav-text"> 服务发现 Discovery</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#eureka-%E8%87%AA%E6%88%91%E4%BF%9D%E6%8A%A4"><span class="nav-text"> Eureka 自我保护</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#zookeeper%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0"><span class="nav-text"> Zookeeper 服务注册与发现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#openfeign%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8"><span class="nav-text"> OpenFeign 服务调用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-text"> 概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#openfeign%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="nav-text"> OpenFeign 使用步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#openfeign%E8%B6%85%E6%97%B6%E6%8E%A7%E5%88%B6"><span class="nav-text"> OpenFeign 超时控制</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#hystrix%E6%96%AD%E8%B7%AF%E5%99%A8"><span class="nav-text"> Hystrix 断路器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0-2"><span class="nav-text"> 概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hystrix%E9%87%8D%E8%A6%81%E6%A6%82%E5%BF%B5"><span class="nav-text"> Hystrix 重要概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E9%99%8D%E7%BA%A7"><span class="nav-text"> 服务降级</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E7%86%94%E6%96%AD"><span class="nav-text"> 服务熔断</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E5%BD%A2%E5%8C%96%E7%9B%91%E6%8E%A7"><span class="nav-text"> 图形化监控</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gateway%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3"><span class="nav-text"> Gateway 服务网关</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0-3"><span class="nav-text"> 概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E5%A4%A7%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5"><span class="nav-text"> 三大核心概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gateway%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B"><span class="nav-text"> Gateway 工作流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%A5%E9%97%A8%E9%85%8D%E7%BD%AE"><span class="nav-text"> 入门配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%80%9A%E8%BF%87%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%90%8D%E5%AE%9E%E7%8E%B0%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1"><span class="nav-text"> 通过微服务名实现动态路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#filter%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text"> Filter 的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#config%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%B5%8B%E8%AF%95"><span class="nav-text"> Config 客户端配置与测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B6%88%E6%81%AF%E9%A9%B1%E5%8A%A8%E4%B9%8B%E6%B6%88%E8%B4%B9%E8%80%85"><span class="nav-text"> 消息驱动之消费者</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#spring-cloud-alibaba-sentinel%E5%AE%9E%E7%8E%B0%E7%86%94%E6%96%AD%E5%92%8C%E9%99%90%E6%B5%81"><span class="nav-text"> Spring Cloud Alibaba Sentinel 实现熔断和限流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#sentinel"><span class="nav-text"> Sentinel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sentinel%E6%8E%A7%E5%88%B6%E5%8F%B0"><span class="nav-text"> Sentinel 控制台</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E6%BC%94%E7%A4%BA%E5%B7%A5%E7%A8%8B"><span class="nav-text"> 初始化演示工程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%81%E6%8E%A7%E8%A7%84%E5%88%99"><span class="nav-text"> 流控规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E8%A7%84%E5%88%99"><span class="nav-text"> 系统规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sentinelresource"><span class="nav-text"> @SentinelResource</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%81%E4%B9%85%E5%8C%96"><span class="nav-text"> 持久化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#spring-cloud-alibaba-seata-%E5%A4%84%E7%90%86%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1"><span class="nav-text"> Spring Cloud Alibaba Seata 处理分布式事务</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1%E9%97%AE%E9%A2%98"><span class="nav-text"> 分布式事务问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#seata%E7%AE%80%E4%BB%8B"><span class="nav-text"> Seata 简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%AF%E5%8A%A8%E4%B8%8E%E9%85%8D%E7%BD%AE"><span class="nav-text"> 启动与配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#at%E5%8E%9F%E7%90%86"><span class="nav-text"> AT 原理</span></a></li></ol></li></ol></li></ol>

    </div>
</div>
            </div>
        
    </div>
</div>


                

            </div>
            
            

        </div>

        <div class="main-content-footer">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">meteor</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv" class="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv" class="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv" class="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            <span class="powered-by-container">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" href="https://hexo.io">Hexo</a> 驱动</span>
                <br>
            <span class="theme-version-container">主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.1.4</a>
        </div>
        
            <div class="icp-info info-item"><a target="_blank" rel="nofollow" href="
                
                    beian.miit.gov.cn
                
                ">冀ICP备20010108号</a></div>
        
        
        
            <div id="start_div" style="display:none">
                2022/8/17 11:45:14
            </div>
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
        
            <script async data-pjax>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    


</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/layouts/navbarShrink.js"></script>

<script src="/js/tools/scrollTopBottom.js"></script>

<script src="/js/tools/lightDarkSwitch.js"></script>





    
<script src="/js/tools/codeBlock.js"></script>




    
<script src="/js/layouts/lazyload.js"></script>




    
<script src="/js/tools/runtime.js"></script>

    
<script src="/js/layouts/odometer.min.js"></script>

    
<link rel="stylesheet" href="/assets/odometer-theme-minimal.css">




  
<script src="/js/libs/Typed.min.js"></script>

  
<script src="/js/plugins/typed.js"></script>







<div class="post-scripts pjax">
    
        
<script src="/js/tools/tocToggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/layouts/toc.js"></script>

<script src="/js/plugins/tabs.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax',
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            Global.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            Global.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            Global.refresh();
        });
    });
</script>




</body>
</html>
